From 0b8d7d1d4b068df46a14895b2f55c00781bd1eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 16 Apr 2024 00:06:39 +0100 Subject: 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. --- tests/unit_node/worker_threads_test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests') diff --git a/tests/unit_node/worker_threads_test.ts b/tests/unit_node/worker_threads_test.ts index 2351e1052..bd600469b 100644 --- a/tests/unit_node/worker_threads_test.ts +++ b/tests/unit_node/worker_threads_test.ts @@ -414,3 +414,25 @@ Deno.test({ mainPort.close(); }, }); + +// Regression test for https://github.com/denoland/deno/issues/23362 +Deno.test("[node/worker_threads] receiveMessageOnPort works if there's pending read", function () { + const { port1, port2 } = new workerThreads.MessageChannel(); + + const message1 = { hello: "world" }; + const message2 = { foo: "bar" }; + + assertEquals(workerThreads.receiveMessageOnPort(port2), undefined); + port2.start(); + + port1.postMessage(message1); + port1.postMessage(message2); + assertEquals(workerThreads.receiveMessageOnPort(port2), { + message: message1, + }); + assertEquals(workerThreads.receiveMessageOnPort(port2), { + message: message2, + }); + port1.close(); + port2.close(); +}); -- cgit v1.2.3