node icon indicating copy to clipboard operation
node copied to clipboard

MessagePort: segfault in receiveMessageOnPort() on a transferred port and in structuredClone() of a closed port

Open ketd opened this issue 1 week ago • 1 comments

Version

v26.13.1 (v24.22.3 and v22.23.5 crash the same way)

Platform

Linux x64 and arm64 (official node:26.13.1-bookworm-slim image) and macOS arm64 (official v26.13.1 binary); v24.22.3 and v22.23.5 checked on Linux x64

Subsystem

worker_threads / messaging

What steps will reproduce the bug?

Two separate scripts. Each one crashes the process with SIGSEGV.

// 1. receiveMessageOnPort() on a port that was just transferred
const { MessageChannel, receiveMessageOnPort } = require('node:worker_threads');
const { port1 } = new MessageChannel();
const other = new MessageChannel();
other.port1.postMessage(null, [port1]);
receiveMessageOnPort(port1); // Segmentation fault
// 2. structuredClone() (without transfer) of a port whose close has completed
const { MessageChannel } = require('node:worker_threads');
const { port1 } = new MessageChannel();
port1.close(() => {
  setImmediate(() => structuredClone(port1)); // Segmentation fault
});

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

  1. receiveMessageOnPort(port1) returns undefined, the same as for a port whose close has completed (MessagePort::ReceiveMessage already returns no_message_symbol when Unwrap() gives nullptr).
  2. structuredClone(port1) throws a DataCloneError. The transfer-list path already handles this case: structuredClone(port1, { transfer: [port1] }) throws "MessagePort in transfer list is already detached". Posting a port right after close(), as in #56362, also throws a DataCloneError ("Cannot clone object of unsupported type.") on v26.13.1.

What do you see instead?

Segmentation fault (exit code 139).

Additional information

  1. MessagePort::TransferForMessaging() calls Close() and then Detach(), which leaves data_ null while the handle is still closing, so Unwrap() still returns the port. MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>&) then calls port->ReceiveMessage(...), which locks data_->mutex_ without checking data_. A data_ check before that call (returning no_message_symbol, as Start() and Stop() already do) fixes it.
  2. After the close completes, the BaseObject is deleted but the object still passes BaseObject::IsBaseObject(), so SerializerDelegate::IsHostObject() reports it as a host object. WriteHostObject(Isolate*, Local<Object>) passes Unwrap<BaseObject>(object) on without a null check, and WriteHostObject(BaseObjectPtr<BaseObject>) calls host_object->GetTransferMode() on nullptr. Treating a null host_object as kDisallowCloneAndTransfer (throwing "Cannot clone object of unsupported type.") fixes it.

The code is the same on main (be0f17cfe4).

ketd avatar Nov 25 '26 02:11 ketd

Reproduced both cases on Linux x64: each script crashes with SIGSEGV on v20.20.2, v22.20.0, v24.22.3, v25.6.1 and main (built from d2812af0), so this isn't a recent regression.

The backtraces on main match the analysis above: case 1 faults in uv_mutex_lock with mutex address 0x8 (a null data_) from MessagePort::ReceiveMessage, and case 2 faults in SerializerDelegate::WriteHostObject.

yohannesjoya avatar Nov 26 '26 18:11 yohannesjoya