Skip to content

perf(wsl): stop the idle profile-state writer thread on WSL hosts - #34545

Merged
nwparker merged 5 commits into
mainfrom
nwparker/wsl-profile-writer-idle-stop
Oct 28, 2026
Merged

nwparker merged 5 commits into
mainfrom
nwparker/wsl-profile-writer-idle-stop

Conversation

@nwparker

@nwparker nwparker commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​283 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​282
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​301 $\color{#cf222e}{\Huge{\mathbf{−}}}$​33 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​268

ELI5

The Orca server inside WSL keeps a background thread whose only job is saving profile state, such as projects, worktrees and layout. It sits idle almost all the time but still holds 13–15 MB. On WSL hosts the thread now shuts down after a minute with nothing to save and starts again, in about 22 ms, the next time something needs saving.

What Changed

  • New option: ProfileStateWorkerAuthority gains writerIdleStopMs. orcad sets it to 60 s only when it runs as a WSL host (isWslHostOrcadProcess). The desktop app and SSH-host orcad don't set it and keep today's always-on writer.
  • Stopping: after 60 s with no command in flight, the authority closes the writer the normal way (close command, exit observed) and remembers the last acknowledged revision.
  • Reopening: the next command reopens the writer at that revision, using the same path maintenance resume uses (reportInitializationFailure), and waits for ready.
    • A running writer still dispatches synchronously, exactly as before.
    • Only a stopped one goes through the reopen.
  • While stopped:
    • assertWritable() counts the authority as writable.
    • close(), pauseForMaintenance()/resume(), quarantineDatabase() and abort() all handle the stopped state. An abort while stopped leaves the authority failed, as it would with a live writer.
  • Faulted writer: it is never stopped, so its failure keeps being reported.
  • Failed reopen never wedges saving. If reopening fails with anything but a revision conflict (worker spawn failure, thread exit, EMFILE), only that command is rejected. The authority then goes back to the stopped state at the same revision, so the next command retries.
    • The first retry is immediate. Repeated failures back off 1 s, 2 s, 4 s… up to 60 s, and spawn nothing while waiting.
    • A success clears the backoff.
    • profile-state-revision-conflict stays terminal, as with maintenance resume.
    • Final and maintenance flushes (the Store signals them with drainBackups(true)) get one reopen attempt even inside a backoff, so shutdown never drops state over a transient failure.
    • Reporting: the reopen client opens with reportInitializationFailure: false. A retriable failure is a console.warn naming the retry delay. Only the terminal revision conflict reaches onFailure ("saving has stopped").
    • This logic lives in ProfileStateWriterReopenPolicy.
  • Reopen visibility: every reopen increments writerReopenCount. A [persistence] Reopened the idle profile writer (N reopens so far) line is logged at most once per 10 minutes. Steady writes just over 60 s apart, which churn the thread, therefore show up in orcad's log without flooding it.
  • Also folded in (nits from the fix(wsl): keep a user's GLIBC_TUNABLES and delegate the launch-env wrapper #34248 review): tests for an empty GLIBC_TUNABLES (treated as unset) and for a misnamed keep-existing entry being refused.

Why

The gate is PSS summed over Orca's process tree in the distro. Measured on a linux-arm64 container build in WSL-host mode (ORCA_WSL_ORCAD_HOST=1, managed launch with warm compile cache, #34187 launch env). Workload: 2 shells, an idle agent-like process, one repo, 120 s idle. Three runs each:

orcad PSS daemon PSS orcad tree PSS (gate) writer thread
main (1d2778f; this PR is now rebased on c791ee2) 168 / 152 / 150 48 219 / 203 / 201 alive
this branch 138 / 138 / 137 48 189 / 189 / 188 stopped

Same-process A/B. One orcad process each time, so run-to-run noise drops out; main's separate runs spread 18 MB. This is orcad's smaps PSS in MB, sampled every 10 s, in 3 processes:

phase process 1 process 2 process 3
writer running (t=10–50 s, 5 samples each) 168 166 167
just after the idle stop (t=80–100 s) 154 153 153
after the next write reopens it (t=130–140 s) 150 152 151
  • The writer itself: stopping it frees 13–14 MB, and the reopen takes back 14–15 MB. Both are steady to within 1 MB inside each process.
  • Unrelated step: a second step of 16–19 MB at t≈110 s, with no writer event, comes from something else settling in the idle process. I'm not claiming it here.

The median tree PSS across separate runs drops by 14 MB, and orcad RSS goes from 174–191 to 160–161 MB. In an earlier probe, starting a new writer took 21–23 ms (×6): worker start, SQLite open and revision check. That cost is paid once, by the first write after a stop.

How often the writer would actually restart:

  • In an earlier probe with writes logged, an idle orcad posted no writes during 120 s after startup, and a CLI reconnect posted none either.
  • Writes come from user actions such as creating a repo, worktree or terminal, or changing the layout.

Not measured:

  • a real paired desktop's steady write rate;
  • the Windows VM working set. That needs awin; it's a secondary number, not the gate.

Why this approach:

  • Moving the writer onto the main thread would block orcad's event loop during SQLite writes.
  • Idle-stop reuses the close-and-reopen-at-revision path that maintenance already relies on.

User-risk:

  • Database changed while stopped: nothing holds the profile database open while the writer is stopped. If another process changes it in that window, reopening refuses the next write with profile-state-revision-conflict and reports it through onFailure. That's the same safe refusal maintenance resume gives today.
  • Failed restart: it surfaces as that one write's rejection after idle, not at startup. Later writes retry, with backoff.
  • Slower first write: the first write after a minute of idle takes about 22 ms longer.

Flag-off: inert unless writerIdleStopMs is set, and only WSL-host orcad sets it.

  • The desktop and SSH hosts run the existing code path. A running writer still dispatches synchronously, and the existing worker-authority tests pass unchanged.
  • This doesn't touch SSH transport or terminal restore.
  • It does change the shared profile-state persistence authority.

Linked Issue

Internal WSL host memory work (D3 bar, PSS gate).

Visual Proof

N/A: no UI change. Persistence internals only.

Testing

  • New profile-state-worker-authority-idle-stop.test.ts, against the real writer worker and SQLite. Each case:

    • no option means no stop, and no idle timer is ever armed;
    • one failed reopen (thread spawn throws EMFILE) rejects only that write; the next write reopens and reaches disk;
    • repeated reopen failures back off without spawning again;
    • a recovered blip never fires onFailure and warns with its retry delay;
    • after two failed reopens, a store final flush inside the backoff still reopens and lands;
    • an idle stop stays writable, reopens for the next write, and idles out again;
    • a write racing the closing stop lands;
    • every reopen is counted, but the reopen line is logged once per window;
    • a database changed while stopped is refused with profile-state-revision-conflict, reported once, and leaves the authority unwritable;
    • closing while stopped, then refusing later writes;
    • maintenance pause and resume while stopped;
    • quarantine while stopped;
    • abort while stopped stays failed.
  • New profile-state-writer-reopen-policy.test.ts: immediate first retry, then exponential backoff capped at a minute; reset on success; revision conflict terminal; reopen log rate limit.

  • orcad-profile-state-startup.test.ts: 60 s on a WSL host, nothing elsewhere.

  • Launch tests: an empty value counts as unset, and a misnamed keep-existing entry is refused.

  • Suites under pnpm test: src/main/persistence/profile-state and src/main/persistence/loading-store/profile-state* pass (987).

    • One failure there is unrelated: the Node → Bun cross-runtime backup integration test.
    • It builds only profile-state-database/-documents, which this PR doesn't touch, and it fails identically when run alone.
  • pnpm tc, pnpm run typecheck:ci, and pnpm run check:code-quality:changed pass.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

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

  • SSH: unchanged, because the option is WSL-host only.
  • Terminal restore: not touched.
  • Desktop: unchanged.
  • Wire compatibility: none involved.

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)

@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

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: 73a159cc-039a-451a-aabf-4b6980257e92
Commits

Reviewing files that changed from the base of the PR and between 7685316 and ebca8c5.

Files selected for processing: 4
  • src/main/persistence/profile-state/profile-state-worker-authority-idle-stop.test.ts
  • src/main/persistence/profile-state/profile-state-worker-authority.ts
  • src/main/persistence/profile-state/profile-state-writer-reopen-policy.test.ts
  • src/main/persistence/profile-state/profile-state-writer-reopen-policy.ts

No actionable comments were generated in the recent review.

Recent 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: ab6cab46-28fe-44f6-bab3-23f90ea67361
Commits

Reviewing files that changed from the base of the PR and between 51ea04c and 7685316.

Files selected for processing: 4
  • src/main/persistence/profile-state/profile-state-worker-authority-idle-stop.test.ts
  • src/main/persistence/profile-state/profile-state-worker-authority.ts
  • src/main/persistence/profile-state/profile-state-writer-reopen-policy.test.ts
  • src/main/persistence/profile-state/profile-state-writer-reopen-policy.ts

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

Walkthrough

The profile-state worker can close its writer after a configured idle delay and reopen it for later commands. Startup configures a 60,000 ms delay for WSL hosts. Tests cover stopped-writer operations and a separate managed-server environment case for empty values and malformed variable names.

Priority: Low

Merge Risk: Moderate · previous review 76853

Resolve the reported static-analysis failure before merging. A durable mutation may also fail when it arrives during an idle stop.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surv… 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: stopping the idle profile-state writer thread on WSL hosts.
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.
Description check Passed The pull request description is detailed and covers ELI5, changes, rationale, testing, visual proof, compatibility notes, and checklist items. It clearly explains the WSL-only behavior, failure handli…
Full details: Description check

Explanation

The pull request description is detailed and covers ELI5, changes, rationale, testing, visual proof, compatibility notes, and checklist items. It clearly explains the WSL-only behavior, failure handling, performance measurements, and test coverage. The linked issue is described as internal work but no issue link or Fixes # reference is provided, and the AI Disclosure section is blank; these omissions do not make the description largely incomplete.

Full details: Docstring Coverage

Explanation

Docstring coverage is 44.44% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 9 functions across 10 files.

Resolution

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

Finishing Touches: 💡 2
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • 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

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

@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: 2


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: f408aaaf-6a5c-4ebf-8a6d-454bd3ba9240
Commits

Reviewing files that changed from the base of the PR and between 84decfd and 51ea04c.

Files selected for processing: 8
  • src/main/managed-server/install/orcad-remote-launch-setsid.test.ts
  • src/main/orcad/orcad-profile-state-startup.test.ts
  • src/main/orcad/orcad-profile-state-startup.ts
  • src/main/persistence/loading-store/profile-state-maintenance-fixture.ts
  • src/main/persistence/profile-state/profile-state-live-store-factory.ts
  • src/main/persistence/profile-state/profile-state-startup-authority.ts
  • src/main/persistence/profile-state/profile-state-worker-authority-idle-stop.test.ts
  • src/main/persistence/profile-state/profile-state-worker-authority.ts

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

Comment on lines +57 to +58
if (this.writer) {
this.writer.assertWritable()

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.

Minor · Functional Correctness · Quick win

Keep durable mutations admissible while the idle writer stops.

If a durable mutation starts after the idle timer calls writer.close() but before the stop completes, this delegation throws profile-state-writer-closed. runDurableMutation calls assertWritable before it submits a write, so the mutation fails instead of waiting for the writer to reopen. Treat an idle stop as writable while preserving genuine writer failures and final-close rejection. (raw.githubusercontent.com)

The writer thread holds ~13-15 MB while orcad sits idle. On WSL hosts the
authority now closes it after a minute without a command and reopens it,
at the last acknowledged revision, for the next one (~22 ms). The desktop
and SSH hosts keep the always-on writer. Also covers an empty and a
misnamed keep-existing launch value.
Writes arriving just over the idle window apart would churn the writer
thread unnoticed. Each reopen now counts, with one log line per ten
minutes so the field shows it.
@nwparker
nwparker force-pushed the nwparker/wsl-profile-writer-idle-stop branch from 51ea04c to 53b7fb2 Compare October 27, 2026 22:23
A failed reopen (spawn failure, thread exit, EMFILE) left the faulted
writer in place, so every later write was refused until orcad restarted.
A non-conflict initialize failure now rejects that one command and puts
the authority back in the stopped state; the next command retries at
once, repeated failures back off up to a minute, and a revision conflict
stays terminal. Abort no longer waits a tick when nothing is stopping.
…y terminal reopen failures

A final or maintenance flush now gets one reopen attempt even inside a
backoff, so shutdown never drops state for a transient spawn failure. A
retriable reopen failure is logged as a warning naming the retry delay
instead of reporting that saving has stopped; only a revision conflict
reaches onFailure.
The no-option case waited in real time to show the writer stayed open.
With no idle timer armed nothing can close it later, so assert that
directly.
@blacksmith-sh

This comment has been minimized.

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