Skip to content

fix(daemon): wake every macOS pool worker each minute so none ages past psynch's half-range - #36373

Open
Fanzzzd wants to merge 2 commits into
stablyai:mainfrom
Fanzzzd:fix/daemon-threadpool-waiter-refresh
Open

Fanzzzd wants to merge 2 commits into
stablyai:mainfrom
Fanzzzd:fix/daemon-threadpool-waiter-refresh

Conversation

@Fanzzzd

@Fanzzzd Fanzzzd commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

On macOS, Node's file worker threads can fall asleep for good after a long run. Terminals then stop taking typing and new terminals time out, but the daemon still answers health checks. This PR wakes every worker once a minute so none of them gets old enough to hit the bug.

What Changed

threadpool-waiter-refresh.ts runs only on macOS in the terminal daemon. Every 60 seconds it posts one blocking read per libuv pool worker on a private FIFO and releases them together 20 ms later. The pool size is read from UV_THREADPOOL_SIZE the way libuv reads it. A worker holding one read cannot take another, so the reads occupy distinct workers. Every parked worker wakes up and parks again at the current psynch generation. If the previous round has not finished, the daemon skips the round. A stuck or saturated pool gets no extra work.

daemon-entry.ts starts it after ready. docs/bug-reproductions/daemon-async-io-stall.md now names the trigger and records the evidence below.

Why

The trigger is the Darwin psynch defect in libuv#5280. A pool worker can stay parked while its siblings finish 2^23 waits. Each wait moves the condvar sequence by 0x100, so that worker ends up exactly 0x80000000 behind. After that the kernel turns every signal into a prepost, U catches L, and pthread_cond_signal returns without a syscall. The pool keeps its queue and its idle workers forever. Timers and sockets don't use the pool, so the daemon still answers RPCs.

I read a wedged production daemon (Node 24.21.0, libuv 1.52.1, macOS, 77 sessions, 2 days up) with an lldb attach that only read memory:

  • The pool queue held 15 uv__fs_work items with idle_threads == 4. The loop's completion queue was empty, its EVFILT_USER wakeup knote was registered, and wq_async.pending was 0. So queued work was never picked up. Completion delivery was fine.
  • On the pool condvar, L = U = 0xfd79d800 and S = 0xfd79d402. Three workers waited at 0xfd79d6..d8. The fourth waited at 0x7d79d800, exactly 0x80000000 behind.

Alternatives considered:

Cost: one 20 ms pause of pool work per minute on macOS, which can delay terminal input or file work by up to 20 ms. Reaching the boundary inside one 60-second interval would take about 140,000 pool operations per second.

Linked Issue

Addresses #11178, the daemon that wedges while every liveness signal stays healthy. This removes the macOS cause for new daemons. It doesn't change readoption, and daemons that are already wedged still need the existing restart review.

Related: #31926, #32220.

Visual Proof

N/A. This changes daemon scheduling only and adds no UI.

Testing

macOS 26.2 arm64:

  • Unit tests: a round really holds the whole pool (a stat posted during it waits for the release; fails with an immediate release), one round per interval, no new round while one is unfinished, no-op off macOS, and pool sizing that matches libuv. Existing daemon-entry, daemon-health, daemon-tcc-attribution, plain-node-entry-guard and daemon-async-io-probe tests pass (93 tests). tsc for node passes. The changed-code quality gate passes.
  • Natural trigger: serial fs.promises.stat on stock Node 24.22.1 with four workers stalled after 40,262,072 operations (21 minutes). The trigger is intermittent. 20M serial stats on two workers and 31M cycles of the libuv#5280 pthread repro completed.
  • Generation witness: lldb read the four idle workers of a Node 24.22.1 process before and after one refresh round. All four wait generations moved to the current value.
  • Red/green pthread model of the pool, aged with 1 ns timed waits on the pool mutex (the method in libuv PR 5351). Without refresh it stalled with 0/4 jobs done after 2^23 + 4,096 steps. With one 1 ms job per worker every 1M steps, all four jobs completed.
  • Not tested: Intel Macs, older macOS, a live daemon running this build for days.

AI Disclosure

Claude Opus 5.5 (Claude Code) did the investigation, the lldb reads, the code and this description.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: Linux and Windows skip it. Their condvars don't use psynch.
  • Remote/SSH: only the local daemon starts it. A relay running on a remote macOS host is not covered.
  • The Electron main process has its own libuv pool and the same exposure. It is not covered here.
  • Mixed versions: no protocol or wire change. Old daemons keep running as they are until they restart.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…st psynch's half-range

A libuv worker that stays parked while its siblings finish 2^23 waits ends
up exactly 0x80000000 behind on Darwin's psynch condvar. From then on every
uv_cond_signal is a userspace no-op (libuv#5280). The pool keeps its queue
and its four idle workers forever. The daemon still answers RPCs, but
terminal input and new terminals stop.

On macOS the daemon now posts one ~1 ms pbkdf2 job per worker every 60
seconds. Each job needs its own worker, so every parked worker wakes and
parks again at the current generation. A round is skipped while the
previous one is still running, so a stuck pool gets no extra work.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Walkthrough

The daemon now starts a Darwin-only threadpool waiter refresh after it reports readiness. The refresh creates blocking FIFO reads for the pool workers and releases them after 20 ms. It skips a round while the previous round remains unfinished. Tests cover scheduling, platform behavior, cleanup, and pool-size parsing. The reproduction document describes the proposed stall mechanism, the refresh procedure, and automated, manual, and natural reproduction observations.

Merge Risk: Moderate · up to 4c348

On macOS, a temporary-directory setup failure can terminate the daemon instead of merely disabling the refresh. Handle that failure before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: periodically waking macOS daemon threadpool workers to avoid the psynch half-range defect.
Description check Passed The description is complete and relevant. It includes the required explanation, changes, rationale, linked issue, visual-proof statement, testing details, AI disclosure, cross-platform notes, and chec…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is complete and relevant. It includes the required explanation, changes, rationale, linked issue, visual-proof statement, testing details, AI disclosure, cross-platform notes, and checklist. It also clearly records known testing limits and uncovered components.

Full details: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 3 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ba6651cd-369b-46f6-b088-0127c3dbaf5f
Commits

Reviewing files that changed from the base of the PR and between 437c47f and 1efae95.

Files selected for processing: 4
  • docs/bug-reproductions/daemon-async-io-stall.md
  • src/main/daemon/daemon-entry.ts
  • src/main/daemon/threadpool-waiter-refresh.test.ts
  • src/main/daemon/threadpool-waiter-refresh.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Fanzzzd

Fanzzzd commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

Longer natural-workload comparison on macOS 26.2 arm64, stock Node 24.22.1 / libuv 1.52.1. Both isolated processes ran serial fs.promises.stat('/tmp') on the default four-worker pool, side by side:

Variant Result
No refresh Stalled after 63,621,610 operations (26 min). No completion for the next 3 minutes.
startThreadpoolWaiterRefresh() at its default 60 s interval Completed 90,000,000 operations (31 min), exit 0

This is one run per side and the trigger is intermittent, so it supports the change but doesn't prove it.

… racing short jobs

Four short pbkdf2 jobs only wake every worker if all four are posted
before the first one finishes. A preempted main thread could let one worker
take two jobs and leave a parked worker untouched.

Each round now posts one blocking read per worker on a private FIFO and
releases them together 20 ms later. A worker holding one read cannot take
another, so the reads occupy distinct workers. A test checks that pool work
posted during a round waits for the release, and it fails when the release
is immediate.

@coderabbitai coderabbitai 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.

Follow-up Comments

Outside diff Handle refresh-factory rejection without an unhandled rejection.

67-110: Minor · Stability & Availability · Quick win

Handle refresh-factory rejection without an unhandled rejection.

If temporary-directory setup fails, createFifoHoldRound() rejects before its try. The detached refresh promise has no rejection handler, so the rejection can remain unhandled after the daemon reports readiness and can terminate the daemon. Handle the rejection and disable this optional mitigation.

Suggested fix
   void (options.createRound ?? createFifoHoldRound)().then((round) => {
     if (stopped || !round) {
       return
     }
@@
     }, options.intervalMs ?? THREADPOOL_WAITER_REFRESH_MS)
     timer.unref()
-  })
+  }, () => {})

Open the source location


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5fb5b0f4-082c-4a76-b6a0-c6669f3a6943
Commits

Reviewing files that changed from the base of the PR and between 1efae95 and 4c34810.

Files selected for processing: 3
  • docs/bug-reproductions/daemon-async-io-stall.md
  • src/main/daemon/threadpool-waiter-refresh.test.ts
  • src/main/daemon/threadpool-waiter-refresh.ts
Files skipped from review as they are similar to previous changes: 3
  • docs/bug-reproductions/daemon-async-io-stall.md
  • src/main/daemon/threadpool-waiter-refresh.test.ts
  • src/main/daemon/threadpool-waiter-refresh.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Fanzzzd

Fanzzzd commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Field data from a daemon running this change (daily build, Node 24.21.0 / libuv 1.52.1, macOS 26.2 arm64, about 57 hours up under normal use). A read-only lldb snapshot:

POOL nthreads=4 idle=4 queued=0 L=0xc089ad00 S=0xc089a900 U=0xc089a900
worker wait generations behind L: 0, 1, 2, 3 waits

L has advanced 0xc089ad (12.6M) waits since start, which is past the 2^23 half-range point. No worker is more than 3 waits behind, and L != U, so signals still reach the kernel. The private FIFO is open in the daemon, and its offset shows the rounds running.

On the 20 ms hold: if a worker isn't scheduled within 20 ms, that round can miss it. The next round comes 60 s later. At this daemon's rate (about 61 waits/s, 3.7k per round), a worker would have to miss about 2,300 consecutive rounds, roughly 38 hours, to reach the boundary. So I kept the fixed hold instead of adding release coordination.

This branch has not been deployed

No deployments
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.

1 participant