-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix process hanging when global onmessage is set in main thread #24496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Setting a global onmessage handler in the main thread (e.g., by the lzma package) was incorrectly keeping the event loop alive and preventing the process from exiting. The issue was in BunWorkerGlobalScope.cpp where adding a message event listener would always call refEventLoop(), even in the main thread. This was intended for worker threads where message listeners should keep the worker alive while waiting for messages from the parent. The fix adds a check to only ref/unref the event loop for message listeners when we're in a worker thread (not the main thread). Main thread message handlers are now correctly treated as passive listeners that don't prevent process exit. Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|
Updated 10:17 PM PT - Nov 7th, 2025
❌ Your commit
🧪 To try this PR locally: bunx bun-pr 24496That installs a local version of the PR into your bun-24496 --bun |
WalkthroughAdds context-aware ref/unref guards to prevent the main thread from remaining alive when an onmessage listener is registered, while preserving correct behavior for worker threads. Includes tests to verify process exit behavior with global onmessage handlers. Changes
Pre-merge checks✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Disabled knowledge base sources:
📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (6)**/*.{cpp,h}📄 CodeRabbit inference engine (.cursor/rules/javascriptcore-class.mdc)
Files:
**/*.cpp📄 CodeRabbit inference engine (.cursor/rules/javascriptcore-class.mdc)
Files:
test/**📄 CodeRabbit inference engine (.cursor/rules/writing-tests.mdc)
Files:
test/js/**/*.{js,ts}📄 CodeRabbit inference engine (.cursor/rules/writing-tests.mdc)
Files:
test/js/web/**/*.{js,ts}📄 CodeRabbit inference engine (.cursor/rules/writing-tests.mdc)
Files:
test/**/*.{js,ts}📄 CodeRabbit inference engine (.cursor/rules/writing-tests.mdc)
Files:
🧠 Learnings (23)📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-10-01T21:59:54.571ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-08-30T00:11:00.890ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-10-01T21:49:27.862ZApplied to files:
📚 Learning: 2025-08-30T00:12:56.803ZApplied to files:
📚 Learning: 2025-08-30T00:12:56.803ZApplied to files:
📚 Learning: 2025-09-08T00:41:12.052ZApplied to files:
📚 Learning: 2025-08-30T00:12:56.803ZApplied to files:
📚 Learning: 2025-11-06T00:58:23.965ZApplied to files:
📚 Learning: 2025-10-19T02:44:46.354ZApplied to files:
📚 Learning: 2025-08-30T00:09:39.100ZApplied to files:
📚 Learning: 2025-10-19T04:55:33.099ZApplied to files:
📚 Learning: 2025-10-18T05:23:24.403ZApplied to files:
📚 Learning: 2025-10-26T01:32:04.844ZApplied to files:
📚 Learning: 2025-08-30T00:12:56.803ZApplied to files:
📚 Learning: 2025-10-08T13:48:02.430ZApplied to files:
📚 Learning: 2025-10-25T17:20:19.041ZApplied to files:
🧬 Code graph analysis (1)test/js/web/workers/onmessage-main-thread.test.ts (1)
🔇 Additional comments (5)
Comment |
Summary
Fixes an issue where requiring packages like
lzmathat set a globalonmessagehandler would prevent the Bun process from exiting.Problem
Setting a global
onmessagehandler in the main thread was incorrectly keeping the event loop alive and preventing the process from terminating. This affected packages likelzmathat detect Web Worker environments by checkingtypeof onmessage !== 'undefined'.Example of the bug:
Root Cause
In
BunWorkerGlobalScope.cpp, adding a message event listener would always callrefEventLoop(), even in the main thread. This was intended for worker threads where message listeners should keep the worker alive while waiting for messages from the parent.Solution
Added a check to only ref/unref the event loop for message listeners when we're in a worker thread (not the main thread). Main thread message handlers are now correctly treated as passive listeners that don't prevent process exit.
Testing
test/js/web/workers/onmessage-main-thread.test.tsonmessagehandlers don't prevent exitonmessagehandlers still work correctlylzmapackage reproduction caseChanges
src/bun.js/bindings/BunWorkerGlobalScope.cpptest/js/web/workers/onmessage-main-thread.test.ts🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]