summaryrefslogtreecommitdiff
path: root/ext/web/13_message_port.js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-04-16 00:06:39 +0100
committerGitHub <noreply@github.com>2024-04-16 01:06:39 +0200
commit0b8d7d1d4b068df46a14895b2f55c00781bd1eef (patch)
tree0a478c09731a8f5c15b3eaee5124dadd39631edc /ext/web/13_message_port.js
parent46c709e52f712ef7e1e41285ec06e9bbbe10c0a5 (diff)
fix(ext/node): panic on 'worker_threads.receiveMessageOnPort' (#23386)
Closes https://github.com/denoland/deno/issues/23362 Previously we were panicking if there was a pending read on a port and `receiveMessageOnPort` was called. This is now fixed by cancelling the pending read, trying to read a message and resuming reading in a loop.
Diffstat (limited to 'ext/web/13_message_port.js')
-rw-r--r--ext/web/13_message_port.js18
1 files changed, 17 insertions, 1 deletions
diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js
index 62c0328c3..4e4184f2a 100644
--- a/ext/web/13_message_port.js
+++ b/ext/web/13_message_port.js
@@ -88,6 +88,9 @@ const MessageChannelPrototype = MessageChannel.prototype;
const _id = Symbol("id");
const MessagePortIdSymbol = _id;
+const MessagePortReceiveMessageOnPortSymbol = Symbol(
+ "MessagePortReceiveMessageOnPort",
+);
const _enabled = Symbol("enabled");
const _refed = Symbol("refed");
const nodeWorkerThreadCloseCb = Symbol("nodeWorkerThreadCloseCb");
@@ -128,6 +131,10 @@ class MessagePort extends EventTarget {
constructor() {
super();
+ ObjectDefineProperty(this, MessagePortReceiveMessageOnPortSymbol, {
+ value: false,
+ enumerable: false,
+ });
ObjectDefineProperty(this, nodeWorkerThreadCloseCb, {
value: null,
enumerable: false,
@@ -189,7 +196,15 @@ class MessagePort extends EventTarget {
this[_id],
);
} catch (err) {
- if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) break;
+ if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) {
+ // If we were interrupted, check if the interruption is coming
+ // from `receiveMessageOnPort` API from Node compat, if so, continue.
+ if (this[MessagePortReceiveMessageOnPortSymbol]) {
+ this[MessagePortReceiveMessageOnPortSymbol] = false;
+ continue;
+ }
+ break;
+ }
nodeWorkerThreadMaybeInvokeCloseCb(this);
throw err;
}
@@ -444,6 +459,7 @@ export {
MessagePort,
MessagePortIdSymbol,
MessagePortPrototype,
+ MessagePortReceiveMessageOnPortSymbol,
nodeWorkerThreadCloseCb,
serializeJsMessageData,
structuredClone,