diff --git a/src/node_messaging.cc b/src/node_messaging.cc index c9b5e324479975..ae60187b6f3ec2 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -90,7 +90,8 @@ MaybeLocal Message::Deserialize(Environment* env, if (ports[i] == nullptr) { for (MessagePort* port : ports) { // This will eventually release the MessagePort object itself. - port->Close(); + if (port != nullptr) + port->Close(); } return MaybeLocal(); } diff --git a/test/parallel/test-worker-messageport-transfer-terminate.js b/test/parallel/test-worker-messageport-transfer-terminate.js new file mode 100644 index 00000000000000..13a30b5c54c6c3 --- /dev/null +++ b/test/parallel/test-worker-messageport-transfer-terminate.js @@ -0,0 +1,18 @@ +// Flags: --experimental-worker +'use strict'; +require('../common'); +const { Worker, MessageChannel } = require('worker_threads'); + +// Check the interaction of calling .terminate() while transferring +// MessagePort objects; in particular, that it does not crash the process. + +for (let i = 0; i < 10; ++i) { + const w = new Worker( + "require('worker_threads').parentPort.on('message', () => {})", + { eval: true }); + setImmediate(() => { + const port = new MessageChannel().port1; + w.postMessage({ port }, [ port ]); + w.terminate(); + }); +}