Skip to content

fix(sidebar): coalesce staggered reconnect refreshes to stop wake freeze (#8539) - #9847

Merged
nwparker merged 1 commit into
mainfrom
nwparker/fix-8539-reconnect-coalesce
Jul 22, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/fix-8539-reconnect-coalesce

Conversation

@nwparker

@nwparker nwparker commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

What & why

Fixes the macOS "app freezes after sleep/wake, must Force-Quit" reports (#8539) for users with remote/SSH sessions.

On wake, remote/SSH runtimes reconnect in a staggered burst — each host coming back online flips onlineRuntimeEnvKey in the sidebar, and the reconnect effect fired a full fetchAllWorktrees() per host. Each call bumps sortEpoch for every repo → a full sidebar + tab-tree remount. With K hosts and N repos that's K×N synchronous remounts stacked on the renderer main thread, and the window wedges. (It's then unkillable because a hung renderer vetoes the window close — that half is a separate follow-up.)

This wraps the reconnect refresh in a single-flight coalescer: the first trigger refreshes immediately (unchanged from today), and any triggers that arrive while a refresh is in flight collapse into one trailing re-run. At any instant at most one refresh runs plus one queued rerun, for any K.

fetchAllWorktrees() and its fetchWorktreeLineage() follow-up are unchanged — same data, same order, same commit path. Only how often the effect fires changes.

ELI5

Your laptop wakes and 20 remote machines reconnect at once. The app used to shout "someone reconnected — reload the entire project list!" 20 times back-to-back, redrawing the whole sidebar 20 times in a row, which locked up the window. Now it says "a bunch reconnected — reload once; if more show up while I'm reloading, reload one more time after." Same end result, no lock-up.

Proof of fix

  • Root cause, measured (repro branch nwparker/repro-8539-wake-storm): a test against the real store shows a staggered 5-runtime wake fires fetchAllWorktrees 5× with 100 sortEpoch bumps, vs 1× / 20 bumps for a single reconnect — an exact K× amplification with zero coalescing.
  • New unit tests (single-flight-coalescer.test.ts, 6 cases): 10 requests arriving during an in-flight run → exactly 1 leading + 1 trailing run; idle requests still run individually; chained bursts, a synchronous throw, and an async rejection are all handled without wedging.

Proof of no regression

  • The whole fetchAllWorktrees().then(fetchWorktreeLineage) unit is coalesced as-is → lineage ordering and worktree data are identical to today; only redundant repeats are dropped.
  • The first trigger still runs immediately (no debounce/delay), so an isolated reconnect or offline flip behaves exactly as before.
  • Effect deps narrowed to [onlineRuntimeEnvKey]; the coalescer captures the fetchAllWorktrees / fetchWorktreeLineage store actions, which are singleton-stable in zustand (verified) — no stale-closure risk.
  • No unmount stop() on purpose: a trailing refresh after unmount just updates the global store (harmless, and matches today's non-cancellation of in-flight refreshes), and the persistent ref avoids a React 18 StrictMode-dev pitfall.
  • Independent adversarial review (GPT-5.6 xhigh) of the actual diff found zero defects / regressions, confirming against source that the zustand actions are stable, lineage ordering is preserved, the single-flight state machine is race-free, and the sibling repoCount effect is unaffected.
  • oxlint, oxfmt, the renderer TypeScript check, and the unit tests all pass.

Scope

Bug A (the freeze) only. The separate "a hung renderer makes the app unkillable" half (the window-close veto) is a follow-up PR — its save-guard state machine is safety-sensitive and shouldn't be bundled here.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a SingleFlightCoalescer utility that limits overlapping async task executions to one active run plus one trailing rerun. Adds tests covering normal, burst, idle, synchronous-throw, and rejection scenarios. Updates the sidebar to use the coalescer for worktree and lineage refreshes triggered by online runtime environment changes.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed, but it omits the required template sections for Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes. Reformat the PR description to match the template and add the missing sections, including screenshots/no visual change, test checklist, review, security, and notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: coalescing sidebar reconnect refreshes to prevent wake freezes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

…eze (#8539)

On wake, remote/SSH runtimes reconnect in a staggered burst; the sidebar refetched all worktrees once per host, piling up K synchronous full-sidebar remounts and freezing the UI. Wrap the reconnect refresh in a single-flight coalescer so at most one refresh runs at a time plus one queued rerun, for any K. fetchAllWorktrees and its lineage follow-up are unchanged; only how often they fire changes.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the nwparker/fix-8539-reconnect-coalesce branch from 56fe4ea to 6fd1dfe Compare July 22, 2026 03:51

@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

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 9d35805e-d436-4d53-aade-97506db1af02

📥 Commits

Reviewing files that changed from the base of the PR and between 56fe4ea and 6fd1dfe.

📒 Files selected for processing (3)
  • src/renderer/src/components/sidebar/index.tsx
  • src/renderer/src/components/sidebar/single-flight-coalescer.test.ts
  • src/renderer/src/components/sidebar/single-flight-coalescer.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/sidebar/single-flight-coalescer.ts

Comment on lines +13 to +14
let resolve!: () => void
let reject!: (e: unknown) => void

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="src/renderer/src/components/sidebar/single-flight-coalescer.test.ts"
wc -l "$file"
cat -n "$file" | sed -n '1,80p'

Repository: stablyai/orca

Length of output: 3100


Document the synchronous initialization here resolve! and reject! rely on the Promise executor assigning them before deferred() returns; add a short comment explaining that constraint.

Source: Coding guidelines

@nwparker
nwparker merged commit 9d3ae3a into main Jul 22, 2026
1 check passed
nwparker added a commit that referenced this pull request Jul 22, 2026
…eze (#8539) (#9847)

Cherry-pick into 1.4.150-release-prep for the stable cut.

On wake, remote/SSH runtimes reconnect in a staggered burst; the sidebar
refetched all worktrees once per host. Wrap reconnect refresh in a
single-flight coalescer so at most one refresh runs at a time plus one
queued rerun.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…eze (stablyai#8539) (stablyai#9847)

On wake, remote/SSH runtimes reconnect in a staggered burst; the sidebar refetched all worktrees once per host, piling up K synchronous full-sidebar remounts and freezing the UI. Wrap the reconnect refresh in a single-flight coalescer so at most one refresh runs at a time plus one queued rerun, for any K. fetchAllWorktrees and its lineage follow-up are unchanged; only how often they fire changes.

Co-authored-by: Orca <[email protected]>
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