Skip to content

Commit 40f807a

Browse files
committed
src: make workers messaging more resilient
1 parent 051741c commit 40f807a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

doc/api/errors.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,10 @@ Used when the main process is trying to read data from the child process's
715715
STDERR/STDOUT, and the data's length is longer than the `maxBuffer` option.
716716

717717
<a id="ERR_CLOSED_MESSAGE_PORT"></a>
718-
### ERR_CLOSED_MESSAGE_PORT
718+
### `ERR_CLOSED_MESSAGE_PORT`
719+
<!--
720+
added: v10.5.0
721+
-->
719722

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

2469-
<a id="ERR_CLOSED_MESSAGE_PORT"></a>
2470-
### `ERR_CLOSED_MESSAGE_PORT`
2471-
<!-- YAML
2472-
added: v10.5.0
2473-
removed: v11.12.0
2474-
-->
2475-
2476-
There was an attempt to use a `MessagePort` instance in a closed
2477-
state, usually after `.close()` has been called.
2478-
24792472
<a id="ERR_CRYPTO_HASH_DIGEST_NO_UTF16"></a>
24802473
### `ERR_CRYPTO_HASH_DIGEST_NO_UTF16`
24812474
<!-- YAML

test/parallel/test-worker-message-port-close.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const common = require('../common');
3-
const { MessageChannel } = require('worker_threads');
3+
const assert = require('assert');
4+
const { MessageChannel, moveMessagePortToContext } = require('worker_threads');
45

56
// Make sure that .start() and .stop() do not throw on closing/closed
67
// MessagePorts.
@@ -29,3 +30,12 @@ function dummy() {}
2930
port1.off('message', dummy);
3031
}));
3132
}
33+
34+
{
35+
const { port2 } = new MessageChannel();
36+
port2.close();
37+
assert.throws(() => moveMessagePortToContext(port2, {}), {
38+
code: 'ERR_CLOSED_MESSAGE_PORT',
39+
message: 'Cannot send data on closed MessagePort'
40+
});
41+
}

0 commit comments

Comments
 (0)