node
node copied to clipboard
Hanging when using `MessagePort` from ES customization hooks via `--import`
Version
v21.1.0
Platform
Microsoft Windows NT 10.0.22631.0 x64
Subsystem
No response
What steps will reproduce the bug?
Running node --import ./bootstrap.mjs ./index.mjs
will hang with the following code.
// hooks.mjs
export async function initialize({ port }) {
port.postMessage('initialize');
}
// bootstrap.mjs
import { register } from 'node:module';
import { MessageChannel } from 'node:worker_threads';
const { port1, port2 } = new MessageChannel();
port1.on('message', (msg) => {
console.log(`msg = ${msg}`);
});
register('./hooks.mjs', {
parentURL: import.meta.url,
data: { port: port2 },
transferList: [port2],
});
// index.mjs
console.log('Hello World');
How often does it reproduce? Is there a required condition?
Reproducible 100% of the time.
What is the expected behavior? Why is that the expected behavior?
For node
to print
$ node --import ./bootstrap.mjs ./index.mjs
msg = initialize
Hello World
and return normally.
What do you see instead?
node
hangs, but prints the expected to the console
$ node --import ./bootstrap.mjs ./index.mjs
msg = initialize
Hello World
Additional information
node
returns normally and no longer hangs if
-
port.close()
is added afterport.postMessage('initialize');
, - or, removing the call to
port1.on('message', ...)
This code was taken and simplified from the example in the documentation here https://nodejs.org/api/module.html#initialize.