src: make workers messaging more resilient

PR-URL: https://github.com/nodejs/node/pull/38510
Fixes: https://github.com/nodejs/node/issues/38499
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Juan José Arboleda
2021-05-02 22:43:08 -05:00
committed by James M Snell
parent b373a2c4ba
commit 75073aba9d
2 changed files with 22 additions and 12 deletions

View File

@@ -715,7 +715,17 @@ 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: REPLACEME
changes:
- version: 11.12.0
pr-url: https://github.com/nodejs/node/pull/26487
description: The error message was removed.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/38510
description: The error message was reintroduced.
-->
There was an attempt to use a `MessagePort` instance in a closed
state, usually after `.close()` has been called.
@@ -2466,16 +2476,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

View File

@@ -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.
@@ -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'
});
}