mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
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:
@@ -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,
|
||||
};
|
||||
|
||||
24
test/regression/issue/26501.test.ts
Normal file
24
test/regression/issue/26501.test.ts
Normal 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();
|
||||
});
|
||||
Reference in New Issue
Block a user