From 4a37180b096e3b69a35dfcf7f8079ea27ffbce08 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 8 Jun 2020 17:42:45 +0200 Subject: [PATCH] worker: emit `'messagerror'` events for failed deserialization This is much nicer than just treating exceptions as uncaught, and enables reporting of exceptions from the internal C++ deserialization machinery. PR-URL: https://github.com/nodejs/node/pull/33772 Backport-PR-URL: https://github.com/nodejs/node/pull/33965 Reviewed-By: Benjamin Gruenbaum --- doc/api/errors.md | 12 +++++++++ doc/api/worker_threads.md | 18 +++++++++++++ lib/internal/worker.js | 4 ++- src/env.h | 2 ++ src/node_errors.h | 4 +++ src/node_messaging.cc | 25 ++++++++++++++++--- ...-transfer-fake-js-transferable-internal.js | 2 +- ...sage-port-transfer-fake-js-transferable.js | 2 +- ...worker-message-port-transfer-filehandle.js | 6 +++++ 9 files changed, 69 insertions(+), 6 deletions(-) diff --git a/doc/api/errors.md b/doc/api/errors.md index 8ed4b31f99aa68..6ba0e36ab74ea9 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -1561,6 +1561,17 @@ behavior. See the documentation for [policy][] manifests for more information. An attempt was made to allocate memory (usually in the C++ layer) but it failed. + +### `ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE` + + +A message posted to a [`MessagePort`][] could not be deserialized in the target +[vm][] `Context`. Not all Node.js objects can be successfully instantiated in +any context at this time, and attempting to transfer them using `postMessage()` +can fail on the receiving side in that case. + ### `ERR_METHOD_NOT_IMPLEMENTED` @@ -2557,6 +2568,7 @@ such as `process.stdout.on('data')`. [`Class: assert.AssertionError`]: assert.html#assert_class_assert_assertionerror [`ERR_INVALID_ARG_TYPE`]: #ERR_INVALID_ARG_TYPE [`EventEmitter`]: events.html#events_class_eventemitter +[`MessagePort`]: worker_threads.html#worker_threads_class_messageport [`Object.getPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf [`Object.setPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf [`REPL`]: repl.html diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 6dfba7a78382a1..250e8f850a03d4 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -303,6 +303,15 @@ input of [`port.postMessage()`][]. Listeners on this event will receive a clone of the `value` parameter as passed to `postMessage()` and no further arguments. +### Event: `'messageerror'` + + +* `error` {Error} An Error object + +The `'messageerror'` event is emitted when deserializing a message failed. + ### `port.close()` + +* `error` {Error} An Error object + +The `'messageerror'` event is emitted when deserializing a message failed. + ### Event: `'online'`