Skip to content

fix(worker): don't keep the main thread alive for a global message listener#32482

Open
0xfandom wants to merge 1 commit into
oven-sh:mainfrom
0xfandom:claude/main-thread-onmessage-exit
Open

fix(worker): don't keep the main thread alive for a global message listener#32482
0xfandom wants to merge 1 commit into
oven-sh:mainfrom
0xfandom:claude/main-thread-onmessage-exit

Conversation

@0xfandom

Copy link
Copy Markdown

What does this PR do?

Fixes #24256. Setting a global message listener on the main thread kept the process running forever instead of exiting:

console.log("hello world");
globalThis.onmessage = () => {};
// Node: prints "hello world" and exits.
// Bun:  prints "hello world" and hangs indefinitely.

WorkerGlobalScope::onDidChangeListenerImpl refs the event loop whenever a message listener is added, so a Worker stays alive to receive messages from its parent. That is correct for worker threads, but on the main thread there is no parent message channel, so the ref is never balanced and the process never exits.

The fix skips the event-loop ref/unref when the global scope is on the main thread (ScriptExecutionContext::isMainThread()). Worker threads still get the keepalive, so they continue to receive messages. The same path backs addEventListener("message", ...), so both spellings are covered.

How did you verify your code works?

Added a parameterized test in test/js/web/workers/worker.test.ts that spawns a main-thread process setting a global message listener via both globalThis.onmessage = and addEventListener("message", ...), and asserts it exits on its own (exitCode === 0, signalCode === null). The test times out (hangs) on the released bun and passes on the debug build.

Also confirmed a Worker that sets globalThis.onmessage still receives messages from its parent (existing worker suite passes: 25 pass / 1 todo).

…stener

Adding a "message" listener to the global scope refs the event loop so a Worker
stays alive to receive messages from its parent. On the main thread there is no
parent, so `globalThis.onmessage = ...` (or addEventListener("message", ...))
kept the process running forever instead of exiting like Node.

Skip the event-loop ref/unref when the global scope is on the main thread;
worker threads still get the keepalive.

Fixes oven-sh#24256

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1024e99c-e1d8-4363-b33d-e05138df2304

📥 Commits

Reviewing files that changed from the base of the PR and between f9530ae and f78efeb.

📒 Files selected for processing (2)
  • src/jsc/bindings/BunWorkerGlobalScope.cpp
  • test/js/web/workers/worker.test.ts

Walkthrough

In WorkerGlobalScope::onDidChangeListenerImpl, a guard is added to retrieve scriptExecutionContext and return early when the context is null or belongs to the main thread. A parameterized regression test verifies that setting onmessage or calling addEventListener("message", ...) on the main thread no longer prevents process exit.

Changes

Main-thread message listener event loop fix

Layer / File(s) Summary
Skip event-loop keepalive on main thread + regression test
src/jsc/bindings/BunWorkerGlobalScope.cpp, test/js/web/workers/worker.test.ts
onDidChangeListenerImpl now returns early when scriptExecutionContext is null or is the main thread, preventing m_messageEventCount ref/unref from running outside worker threads. A test.each case spawns a Bun process with each of the two main-thread message listener forms and asserts exit code 0, stdout "ready", and no stderr errors.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main fix: preventing a global message listener on the main thread from keeping the process alive.
Description check ✅ Passed The description completely covers both required sections with clear explanations of the issue, the fix, and the verification approach.
Linked Issues check ✅ Passed The PR directly addresses issue #24256 by fixing the root cause: skipping event-loop ref/unref on the main thread when handling message listeners.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the issue described in #24256; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bun process doesn’t exit when globalThis.onmessage is set

1 participant