Skip to content

worker_threads parentPort extends EventTarget in Node 14 but extends EventEmitter in Node 12 #35835

Closed
@snowinferno

Description

@snowinferno
  • Version: v14.15.0
  • Platform: Darwin SuperSecretHostname 19.6.0 Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64 x86_64
  • Subsystem: worker_threads

What steps will reproduce the bug?

Run this script under node v14.15.0

const { isMainThread, parentPort, Worker } = require('worker_threads');

if (isMainThread) {
    const worker = new Worker(__filename);
    worker.on('error', (message) => console.dir(message));
} else {
    parentPort.emit('error', "This is my error message.");
}

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

Every time the script is run.

Required condition: Node v14.15.0

What is the expected behavior?

The error should reflect Error [ERR_UNHANDLED_ERROR]: Unhandled error. ('This is my error message.') with a stack trace.

What do you see instead?

The error reflects TypeError [Error]: parentPort.emit is not a function with a stack trace.
parentPort seemingly does not extend EventEmitter as documentation indicates and behavior under node v12.19.0 shows.

Additional information

This appears to be related to commit 0aa3809b6be on the v14.x branch

This would be a breaking change in a stable API since this same script behaves as expected under Node v12.19.0.

Inspecting parentPort I find that contrary to expectation and documentation, it extends EventTarget instead of EventEmitter.

MessagePort [EventTarget] {
  [Symbol(kEvents)]: Map(2) {
    'newListener' => { size: 1, next: [Listener] },
    'removeListener' => { size: 1, next: [Listener] }
  },
  [Symbol(kMaxListeners)]: 10,
  [Symbol(kMaxListenersWarned)]: false,
  [Symbol(kNewListener)]: [Function (anonymous)],
  [Symbol(kRemoveListener)]: [Function (anonymous)]
}

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