Skip to content

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

Backlog

Description

@ketd

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 port.postMessage with another closed port results in an abort #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 (be0f17c).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions