Skip to content

port.postMessage with another closed port results in an abort #56362

Open
@zyscoder

Description

@zyscoder

Version

v22.11.0

Platform

Linux u24vm 6.8.0-50-generic #51-Ubuntu SMP PREEMPT_DYNAMIC Sat Nov  9 17:58:29 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

worker_threads

What steps will reproduce the bug?

Setup a node instance,

» node

and run the following javascript code.

worker_threads = require('worker_threads');
msg1 = new worker_threads.MessageChannel();
msg1.port1.close();
msg2 = new worker_threads.MessageChannel();
msg2.port1.postMessage(msg1.port1);

Then the node instance occurs an abort.

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

This abort can always be triggered following the steps above.

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

If any error occurs, an exception or other similar error-reporting stuff should be thrown and then caught and handled correctly. There is no reason to abort the whole node process.

What do you see instead?

» node                                                                                                        
Welcome to Node.js v22.11.0.
Type ".help" for more information.
>
> worker_threads = require('worker_threads');
{
  isMainThread: true,
  MessagePort: [Function: MessagePort],
  MessageChannel: [Function: MessageChannel],
  markAsUncloneable: [Function: markAsUncloneable],
  markAsUntransferable: [Function: markAsUntransferable],
  isMarkedAsUntransferable: [Function: isMarkedAsUntransferable],
  moveMessagePortToContext: [Function: moveMessagePortToContext],
  receiveMessageOnPort: [Function: receiveMessageOnPort],
  resourceLimits: {},
  postMessageToThread: [AsyncFunction: postMessageToThread],
  threadId: 0,
  SHARE_ENV: Symbol(nodejs.worker_threads.SHARE_ENV),
  Worker: [class Worker extends EventEmitter],
  parentPort: null,
  workerData: null,
  BroadcastChannel: [class BroadcastChannel extends EventTarget],
  setEnvironmentData: [Function: setEnvironmentData],
  getEnvironmentData: [Function: getEnvironmentData]
}
> msg1 = new worker_threads.MessageChannel();
MessageChannel {
  port1: MessagePort [EventTarget] {
    active: true,
    refed: false,
    [Symbol(kEvents)]: SafeMap(2) [Map] {
      'newListener' => [Object],
      'removeListener' => [Object]
    },
    [Symbol(events.maxEventTargetListeners)]: 10,
    [Symbol(events.maxEventTargetListenersWarned)]: false,
    [Symbol(kHandlers)]: SafeMap(0) [Map] {},
    [Symbol(kNewListener)]: [Function (anonymous)],
    [Symbol(kRemoveListener)]: [Function (anonymous)],
    [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
  },
  port2: MessagePort [EventTarget] {
    active: true,
    refed: false,
    [Symbol(kEvents)]: SafeMap(2) [Map] {
      'newListener' => [Object],
      'removeListener' => [Object]
    },
    [Symbol(events.maxEventTargetListeners)]: 10,
    [Symbol(events.maxEventTargetListenersWarned)]: false,
    [Symbol(kHandlers)]: SafeMap(0) [Map] {},
    [Symbol(kNewListener)]: [Function (anonymous)],
    [Symbol(kRemoveListener)]: [Function (anonymous)],
    [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
  }
}
> msg1.port1.close();
undefined
> msg2 = new worker_threads.MessageChannel();
MessageChannel {
  port1: MessagePort [EventTarget] {
    active: true,
    refed: false,
    [Symbol(kEvents)]: SafeMap(2) [Map] {
      'newListener' => [Object],
      'removeListener' => [Object]
    },
    [Symbol(events.maxEventTargetListeners)]: 10,
    [Symbol(events.maxEventTargetListenersWarned)]: false,
    [Symbol(kHandlers)]: SafeMap(0) [Map] {},
    [Symbol(kNewListener)]: [Function (anonymous)],
    [Symbol(kRemoveListener)]: [Function (anonymous)],
    [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
  },
  port2: MessagePort [EventTarget] {
    active: true,
    refed: false,
    [Symbol(kEvents)]: SafeMap(2) [Map] {
      'newListener' => [Object],
      'removeListener' => [Object]
    },
    [Symbol(events.maxEventTargetListeners)]: 10,
    [Symbol(events.maxEventTargetListenersWarned)]: false,
    [Symbol(kHandlers)]: SafeMap(0) [Map] {},
    [Symbol(kNewListener)]: [Function (anonymous)],
    [Symbol(kRemoveListener)]: [Function (anonymous)],
    [Symbol(nodejs.internal.kCurrentlyReceivingPorts)]: undefined
  }
}
> msg2.port1.postMessage(msg1.port1);
[1]    1295254 segmentation fault (core dumped)  node

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    workerIssues and PRs related to Worker support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions