Skip to content

Commit

Permalink
src: make workers messaging more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
juanarbol committed May 3, 2021
1 parent 051741c commit a73a147
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void MessagePort::MoveToContext(const FunctionCallbackInfo<Value>& args) {
}
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
if (port == nullptr || port->IsHandleClosing()) {
Isolate* isolate = env->isolate();
Isolate* isozlate = env->isolate();
THROW_ERR_CLOSED_MESSAGE_PORT(isolate);
return;
}
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-worker-message-port-close.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const common = require('../common');
const { MessageChannel } = require('worker_threads');
const assert = require('assert');
const { MessageChannel, moveMessagePortToContext } = require('worker_threads');

// Make sure that .start() and .stop() do not throw on closing/closed
// MessagePorts.
Expand Down Expand Up @@ -29,3 +30,12 @@ function dummy() {}
port1.off('message', dummy);
}));
}

{
const { port2 } = new MessageChannel();
port2.close();
assert.throws(() => moveMessagePortToContext(port2, {}), {
code: 'ERR_CLOSED_MESSAGE_PORT',
message: 'Cannot send data on closed MessagePort'
});
}

0 comments on commit a73a147

Please sign in to comment.