Skip to content

Commit 51b0797

Browse files
veeceeyclaude
andauthored
fix: prevent createDeferred from keeping Node.js process alive (#2588)
* fix: prevent createDeferred from keeping Node.js process alive The scheduler uses MessageChannel internally for task scheduling. In Node.js (with -C browser flag), active MessagePort listeners keep the event loop alive even after all work is done and dispose() has been called. This causes the process to hang indefinitely. The fix calls unref() on both MessageChannel ports so they don't prevent natural process exit. The unref method is Node.js-specific and doesn't exist in browsers, so we guard the call with a typeof check. Fixes #2570 * add changeset for createDeferred node hang fix Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a0524c0 commit 51b0797

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

.changeset/cyan-comics-worry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
fix: prevent createDeferred from keeping Node.js process alive

packages/solid/src/reactive/scheduler.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ const maxSigned31BitInt = 1073741823;
2929
function setupScheduler() {
3030
const channel = new MessageChannel(),
3131
port = channel.port2;
32+
// In Node.js, active MessagePort listeners keep the event loop alive.
33+
// Calling unref() allows the process to exit naturally when there is no
34+
// other work keeping it alive (e.g. after dispose). unref is not available
35+
// in browsers, so we guard the call.
36+
if (typeof channel.port1.unref === "function") channel.port1.unref();
37+
if (typeof port.unref === "function") port.unref();
3238
scheduleCallback = () => port.postMessage(null);
3339
channel.port1.onmessage = () => {
3440
if (scheduledCallback !== null) {

0 commit comments

Comments
 (0)