-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix native module exit issue without arbitrary thresholds (#24484) #24531
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
) - Add shouldExitProcess() method that allows exit when no JS tasks remain - Replace isEventLoopAlive() with shouldExitProcess() in main event loop - Handles native modules like lzma that create background threads - Follows Node.js behavior: native modules don't prevent process exit - Removes arbitrary threshold approach that was rejected This fix allows the process to exit when: 1. No explicit JavaScript tasks remain (timers, promises, etc.) 2. Only background handles from native modules are active 3. The process is effectively idle from a JavaScript perspective Resolves oven-sh#24484
|
@Jarred-Sumner Is this Okay now is this approach valid? |
WalkthroughModified event loop termination logic in bun.js.zig to use a new Changes
Possibly related PRs
Suggested reviewers
Pre-merge checks✅ Passed checks (4 passed)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Disabled knowledge base sources:
📒 Files selected for processing (3)
🧰 Additional context used📓 Path-based instructions (3)**/*.zig📄 CodeRabbit inference engine (.cursor/rules/javascriptcore-class.mdc)
Files:
src/**/*.zig📄 CodeRabbit inference engine (.cursor/rules/building-bun.mdc)
Files:
src/bun.js/**/*.zig📄 CodeRabbit inference engine (.cursor/rules/zig-javascriptcore-classes.mdc)
Files:
🧠 Learnings (13)📓 Common learnings📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-08-30T00:11:57.076ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-11-08T04:06:33.198ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-11-06T00:58:23.965ZApplied to files:
📚 Learning: 2025-08-30T00:13:36.815ZApplied to files:
📚 Learning: 2025-08-30T00:11:57.076ZApplied to files:
📚 Learning: 2025-10-16T02:17:35.237ZApplied to files:
🔇 Additional comments (4)
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. Comment |
|
@Jarred-Sumner You can review this PR now! |
Fix native module exit issue without arbitrary thresholds (#24484)
Problem
The
lzmalibrary (and other native modules) causes Bun to hang indefinitely after script completion. The process never terminates naturally and requires manual interruption.Reproduction:
Root Cause
Native modules like
lzmacreate background threads or system handles that keep the event loop active. The main event loop termination logic usesvm.isEventLoopAlive()which only checks for explicit JavaScript tasks, not accounting for these background handles.Solution
No arbitrary thresholds - The previous approach using
active_count <= 2was correctly rejected as arbitrary.Instead, this fix implements a principled approach:
shouldExitProcess()method that allows exit when no JavaScript tasks remainisEventLoopAlive()calls in main event loop with!shouldExitProcess()Key Logic
Changes
src/bun.js/VirtualMachine.zigshouldExitProcess()method that allows exit when JavaScript is idlesrc/bun.js.zigwhile (vm.isEventLoopAlive())withwhile (!vm.shouldExitProcess())while (vm.isEventLoopAlive())withwhile (!vm.shouldExitProcess())Testing
Before fix:
After fix:
Impact
lzmaand similar native modulesRelated Issues
Closes #24484