From b373a2c4babc213b79b66f6ba36e8a951cae5ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Arboleda?= Date: Mon, 3 May 2021 13:12:39 -0500 Subject: [PATCH] Revert "worker: remove `ERR_CLOSED_MESSAGE_PORT`" This reverts commit 73370b45844fd64e9ca8f9270220e425f9ef8ab6. The unit test is preserved to make sure it does not break https://github.com/nodejs/node/issues/26463 again. PR-URL: https://github.com/nodejs/node/pull/38510 Fixes: https://github.com/nodejs/node/issues/38499 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: James M Snell --- doc/api/errors.md | 6 ++++++ src/node_errors.h | 2 ++ src/node_messaging.cc | 6 +++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index e8b416949da7c8..b8dfeb4e53d573 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -714,6 +714,12 @@ Used when a child process is being forked without specifying an IPC channel. 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. + +### ERR_CLOSED_MESSAGE_PORT + +There was an attempt to use a `MessagePort` instance in a closed +state, usually after `.close()` has been called. + ### `ERR_CONSOLE_WRITABLE_STREAM` diff --git a/src/node_errors.h b/src/node_errors.h index 291365fa3b4dc9..96659f3a400826 100644 --- a/src/node_errors.h +++ b/src/node_errors.h @@ -32,6 +32,7 @@ void OnFatalError(const char* location, const char* message); V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \ V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \ V(ERR_BUFFER_TOO_LARGE, Error) \ + V(ERR_CLOSED_MESSAGE_PORT, Error) \ V(ERR_CONSTRUCT_CALL_REQUIRED, TypeError) \ V(ERR_CONSTRUCT_CALL_INVALID, TypeError) \ V(ERR_CRYPTO_INITIALIZATION_FAILED, Error) \ @@ -118,6 +119,7 @@ ERRORS_WITH_CODE(V) #define PREDEFINED_ERROR_MESSAGES(V) \ V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \ "Buffer is not available for the current Context") \ + V(ERR_CLOSED_MESSAGE_PORT, "Cannot send data on closed MessagePort") \ V(ERR_CONSTRUCT_CALL_INVALID, "Constructor cannot be called") \ V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \ V(ERR_CRYPTO_INITIALIZATION_FAILED, "Initialization failed") \ diff --git a/src/node_messaging.cc b/src/node_messaging.cc index fbe6c407a6cea2..ac4abc3af01568 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -1065,7 +1065,11 @@ void MessagePort::MoveToContext(const FunctionCallbackInfo& args) { "The \"port\" argument must be a MessagePort instance"); } MessagePort* port = Unwrap(args[0].As()); - CHECK_NOT_NULL(port); + if (port == nullptr || port->IsHandleClosing()) { + Isolate* isolate = env->isolate(); + THROW_ERR_CLOSED_MESSAGE_PORT(isolate); + return; + } Local context_arg = args[1]; ContextifyContext* context_wrapper;