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>
This commit is contained in:
Claude Bot
2026-01-27 14:49:37 +00:00
parent bfe40e8760
commit 89e2a41958
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();
});