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 6eea0f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
15 changes: 4 additions & 11 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ Used when the main process is trying to read data from the child process's
STDERR/STDOUT, and the data's length is longer than the `maxBuffer` option.

<a id="ERR_CLOSED_MESSAGE_PORT"></a>
### ERR_CLOSED_MESSAGE_PORT
### `ERR_CLOSED_MESSAGE_PORT`
<!--
added: v10.5.0
-->

There was an attempt to use a `MessagePort` instance in a closed
state, usually after `.close()` has been called.
Expand Down Expand Up @@ -2466,16 +2469,6 @@ removed: v12.5.0
The value passed to `postMessage()` contained an object that is not supported
for transferring.

<a id="ERR_CLOSED_MESSAGE_PORT"></a>
### `ERR_CLOSED_MESSAGE_PORT`
<!-- YAML
added: v10.5.0
removed: v11.12.0
-->

There was an attempt to use a `MessagePort` instance in a closed
state, usually after `.close()` has been called.

<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
### `ERR_CRYPTO_HASH_DIGEST_NO_UTF16`
<!-- YAML
Expand Down
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 6eea0f2

Please sign in to comment.