Skip to content

Recover a wedged gRPC channel in place instead of hanging until app restart (#1672) - #1880

Merged
mstomar125 merged 1 commit into
mainfrom
issue-1672-channel-recover-v2
Aug 1, 2026
Merged

mstomar125 merged 1 commit into
mainfrom
issue-1672-channel-recover-v2

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Recovers a wedged gRPC channel in place instead of hanging until the user restarts the app — the mechanism behind the reopened #1672 (Daily Journal stuck on "Loading…" forever).

The desktop app rides every service client and the WatchNodes stream on a single shared h2 channel. A stream churn during heavy sync can wedge that connection: reads/writes and the stream hang indefinitely (the lazy channel has no client-side timeout) even though the daemon socket stays healthy — a freshly-dialed client answers fine. So the socket-based check_daemon_status keeps reporting healthy, onDaemonReconnect never fires, and panes stay stuck. Only a full app restart (fresh channel) recovered it.

What changed

Recover every surface on the shared channel, the way a restart would:

  • GrpcClient::reconnect() + swap_channel() (services/grpc_client.rs) — rebuild the lazy channel and every service client from scratch, preserving the active-database routing and this window's client_id (echo suppression is scoped by it — a fresh id would make our own writes re-appear as remote changes), then bump db_generation so the node-event watcher re-opens its WatchNodes stream on the fresh channel.
  • probe_and_recover_channel command (commands/nodes.rs) — one lightweight get_node bounded by a 5 s timeout: any completion (incl. NotFound) proves the channel is live → false; a timeout means it is wedged → reconnect() + re-probe → true only if the rebuilt channel answers.
  • daemon-status steady-state poll (daemon-status.ts) — after a healthy status, probe the real channel; if it had to be rebuilt, re-fire the reconnect listeners so panes re-fetch (applyStatus won't — the daemon never left healthy).
  • ai-chat token stream (commands/local_agent.rs) — now select!s on db_generation (biased), mirroring the node watcher, so a rebuild drops its wedged stream and it re-subscribes on the fresh channel instead of hanging forever.
  • ProClient::rebind() (services/pro_client.rs, called from the probe) — points the cached Pro cloud-sync client at the rebuilt channel so unary cloud-sync calls stop riding the dead connection.

Adversarial review

An adversarial review of the first cut confirmed the core mechanics (lock discipline, watcher re-subscription, client_id preservation) and caught that the Pro cloud-sync and ai-chat surfaces were left on the dead channel — addressed above. The Pro live-status WatchSyncStatus stream still needs coordinated respawn-on-recovery (task interruption + STREAM_SPAWNED reset + frontend onDaemonReconnect re-subscribe); tracked as a focused follow-up in #1879.

Testing

  • bun run test daemon-status suite: 17/17, including a new test that drives the steady-state poll into a wedged channel and asserts it rebuilds + re-fires onDaemonReconnect.
  • Full pre-push gate green: test:all (frontend + skill + Rust) + cargo build --bin nodespaced + test:e2e (headless daemon round-trip) + cargo test -p nodespace-app (Tauri-seam, ADR-048).
  • cargo build -p nodespace-app clean; the token-stream and Pro-rebind additions follow the node watcher's proven db_generation interruption pattern.
  • The live timing-dependent wedge is hard to force deterministically; the recovery path is covered by the unit test above and the happy-path launch is unaffected (extra probe RPC per 15 s poll returns NotFound immediately → no-op).

Closes #1672.

Co-Authored-By: Claude Opus 4.8 [email protected]

…estart (#1672)

The desktop app rides every service client and long-lived stream on a
single shared h2 channel. A stream churn during a heavy sync can wedge that
connection: reads/writes and streams hang forever (the lazy channel has no
client-side timeout) even though the daemon socket stays healthy — a
freshly-dialed client answers fine. The socket-based daemon-status check
therefore keeps reporting "healthy", onDaemonReconnect never fires, and the
journal stays stuck on "Loading…"; only a full app restart (fresh channel)
recovered it.

Recover in place, the way a restart would, across every surface on the channel:

- GrpcClient::reconnect() rebuilds the lazy channel and every service client
  from scratch, preserving the active-database routing AND this window's
  client_id (echo suppression is scoped by it), and bumps db_generation so the
  node-event watcher re-opens its WatchNodes stream on the fresh channel.
- probe_and_recover_channel runs one lightweight RPC bounded by a short
  timeout: any completion (incl NotFound) proves the channel is live; a timeout
  means it is wedged, so it calls reconnect() and re-probes.
- The daemon-status steady-state poll calls the probe after a healthy status
  and, when the channel had to be rebuilt, re-fires the reconnect listeners so
  panes re-fetch — applyStatus won't, since the daemon never left "healthy".
- The ai-chat token stream now selects on db_generation (biased), mirroring the
  watcher, so a rebuild drops its wedged stream and it re-subscribes on the
  fresh channel instead of hanging forever.
- ProClient::rebind() points the cached Pro cloud-sync client at the rebuilt
  channel, so unary cloud-sync calls stop riding the dead connection.

The Pro live-status WatchSyncStatus forwarding task still needs coordinated
respawn on recovery — tracked in #1879.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

APPROVE (self-review, author=reviewer per merge policy).

Adversarial review of the first cut CONFIRMED the core mechanics — lock discipline (no lock held across the probe/reconnect awaits), watcher re-subscription on the new channel (swap_channel installs new clients before bumping db_generation; biased select drops the wedged stream), and client_id preservation across the rebuild (echo suppression stays scoped). It also caught two real incompleteness defects, both now fixed in this PR:

  1. ProClient (cloud-sync, separate managed state) kept its own clone of the dead channel → ProClient::rebind() from the probe re-points it.
  2. ai-chat token stream hung on the wedged stream.next() → now biased-select!s on db_generation, mirroring the node watcher.

Remaining coordinated piece (Pro live-status WatchSyncStatus stream auto-respawn) is right-sized out to a focused follow-up (#1879). Full pre-push gate green (4098 frontend + scripts/skill + Rust workspace + e2e round-trip + Tauri-seam ADR-048), rebased onto latest main (c2edd69).

@mstomar125
mstomar125 merged commit 3350472 into main Aug 1, 2026
2 checks passed
@mstomar125
mstomar125 deleted the issue-1672-channel-recover-v2 branch August 1, 2026 09:43
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.

[P0][bug] Daily Journal hangs on "Loading…" forever on a fresh empty DB (clean install, signed out)

1 participant