Compare commits

...

1 Commits

Author SHA1 Message Date
Claude Bot
89e2a41958 fix(worker_threads): receiveMessageOnPort handles falsy values
The `receiveMessageOnPort` function was using `if (!res)` to check for
no message available, which incorrectly treated falsy message values
(0, false, '', null) as "no message" and returned undefined.

Changed to `if (res === undefined)` to correctly distinguish between
"no message available" (undefined from native code) and "message with
a falsy value".

Fixes #26501

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-27 15:02:51 +00:00
2 changed files with 25 additions and 1 deletions

View File

@@ -121,7 +121,7 @@ let workerData = _workerData;
let threadId = _threadId;
function receiveMessageOnPort(port: MessagePort) {
let res = _receiveMessageOnPort(port);
if (!res) return undefined;
if (res === undefined) return undefined;
return {
message: res,
};

View File

@@ -0,0 +1,24 @@
import { expect, test } from "bun:test";
import { MessageChannel, receiveMessageOnPort } from "node:worker_threads";
test("receiveMessageOnPort handles falsy values correctly", () => {
const { port1, port2 } = new MessageChannel();
const values = [0, 1, false, true, "", "hello world", null];
for (const value of values) {
port1.postMessage(value);
}
const received: unknown[] = [];
for (let i = 0; i < values.length; i++) {
const result = receiveMessageOnPort(port2);
if (result !== undefined) {
received.push(result.message);
}
}
expect(received).toEqual(values);
// Extra call should return undefined (no more messages)
expect(receiveMessageOnPort(port2)).toBeUndefined();
});