Skip to content

Commit 73370b4

Browse files
addaleaxBridgeAR
authored andcommitted
worker: remove ERR_CLOSED_MESSAGE_PORT
This aligns `MessagePort`s more with the web API. Refs: #26463 PR-URL: #26487 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9398d84 commit 73370b4

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

Diff for: doc/api/errors.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,6 @@ Used when a child process is being forked without specifying an IPC channel.
688688
Used when the main process is trying to read data from the child process's
689689
STDERR/STDOUT, and the data's length is longer than the `maxBuffer` option.
690690

691-
<a id="ERR_CLOSED_MESSAGE_PORT"></a>
692-
### ERR_CLOSED_MESSAGE_PORT
693-
694-
There was an attempt to use a `MessagePort` instance in a closed
695-
state, usually after `.close()` has been called.
696-
697691
<a id="ERR_CONSOLE_WRITABLE_STREAM"></a>
698692
### ERR_CONSOLE_WRITABLE_STREAM
699693

@@ -1975,6 +1969,16 @@ A module file could not be resolved while attempting a [`require()`][] or
19751969
> Stability: 0 - Deprecated. These error codes are either inconsistent, or have
19761970
> been removed.
19771971
1972+
<a id="ERR_CLOSED_MESSAGE_PORT"></a>
1973+
### ERR_CLOSED_MESSAGE_PORT
1974+
<!-- YAML
1975+
added: v10.5.0
1976+
removed: REPLACEME
1977+
-->
1978+
1979+
There was an attempt to use a `MessagePort` instance in a closed
1980+
state, usually after `.close()` has been called.
1981+
19781982
<a id="ERR_HTTP2_FRAME_ERROR"></a>
19791983
### ERR_HTTP2_FRAME_ERROR
19801984
<!-- YAML

Diff for: src/node_errors.h

-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ void FatalException(const v8::FunctionCallbackInfo<v8::Value>& args);
4444
V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \
4545
V(ERR_BUFFER_TOO_LARGE, Error) \
4646
V(ERR_CANNOT_TRANSFER_OBJECT, TypeError) \
47-
V(ERR_CLOSED_MESSAGE_PORT, Error) \
4847
V(ERR_CONSTRUCT_CALL_REQUIRED, Error) \
4948
V(ERR_INVALID_ARG_VALUE, TypeError) \
5049
V(ERR_INVALID_ARG_TYPE, TypeError) \
@@ -87,7 +86,6 @@ void FatalException(const v8::FunctionCallbackInfo<v8::Value>& args);
8786
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
8887
"Buffer is not available for the current Context") \
8988
V(ERR_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\
90-
V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort") \
9189
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
9290
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
9391
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \

Diff for: src/node_messaging.cc

-2
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,6 @@ void MessagePort::Start(const FunctionCallbackInfo<Value>& args) {
729729
MessagePort* port;
730730
ASSIGN_OR_RETURN_UNWRAP(&port, args.This());
731731
if (!port->data_) {
732-
THROW_ERR_CLOSED_MESSAGE_PORT(env);
733732
return;
734733
}
735734
port->Start();
@@ -741,7 +740,6 @@ void MessagePort::Stop(const FunctionCallbackInfo<Value>& args) {
741740
CHECK(args[0]->IsObject());
742741
ASSIGN_OR_RETURN_UNWRAP(&port, args[0].As<Object>());
743742
if (!port->data_) {
744-
THROW_ERR_CLOSED_MESSAGE_PORT(env);
745743
return;
746744
}
747745
port->Stop();

Diff for: test/parallel/test-worker-message-port-close.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { MessageChannel } = require('worker_threads');
4+
5+
// Make sure that .start() and .stop() do not throw on closing/closed
6+
// MessagePorts.
7+
// Refs: https://github.com/nodejs/node/issues/26463
8+
9+
function dummy() {}
10+
11+
{
12+
const { port1, port2 } = new MessageChannel();
13+
port1.close(common.mustCall(() => {
14+
port1.on('message', dummy);
15+
port1.off('message', dummy);
16+
port2.on('message', dummy);
17+
port2.off('message', dummy);
18+
}));
19+
port1.on('message', dummy);
20+
port1.off('message', dummy);
21+
port2.on('message', dummy);
22+
port2.off('message', dummy);
23+
}
24+
25+
{
26+
const { port1 } = new MessageChannel();
27+
port1.on('message', dummy);
28+
port1.close(common.mustCall(() => {
29+
port1.off('message', dummy);
30+
}));
31+
}

0 commit comments

Comments
 (0)