worker: do not send message if port is closing

Fixes: https://github.com/nodejs/node/issues/42296

PR-URL: https://github.com/nodejs/node/pull/42357
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Rich Trott
2022-03-16 06:16:00 +00:00
parent a66b9cabc8
commit 44131ad638
2 changed files with 9 additions and 1 deletions

View File

@@ -980,7 +980,7 @@ void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
// Even if the backing MessagePort object has already been deleted, we still
// want to serialize the message to ensure spec-compliant behavior w.r.t.
// transfers.
if (port == nullptr) {
if (port == nullptr || port->IsHandleClosing()) {
Message msg;
USE(msg.Serialize(env, context, args[0], transfer_list, obj));
return;

View File

@@ -39,3 +39,11 @@ function dummy() {}
message: 'Cannot send data on closed MessagePort'
});
}
// Refs: https://github.com/nodejs/node/issues/42296
{
const ch = new MessageChannel();
ch.port1.onmessage = common.mustNotCall();
ch.port2.close();
ch.port2.postMessage('fhqwhgads');
}