Skip to content

fix(remote): shared-control replay-window subscription leak, benign snapshot banner, viewport clear - #8091

Closed
Jinwoo-H wants to merge 3 commits into
mainfrom
fix/remote-client-subscription-and-error-polish
Closed

Jinwoo-H wants to merge 3 commits into
mainfrom
fix/remote-client-subscription-and-error-polish

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Three small, independently-committed remote-terminal / shared-control client fixes:

  1. Shared-control subscription leak on close-during-replay (fix(remote): defer close during shared-control replay window…). On reconnect, replaySharedControlSubscriptions clears remoteSubscriptionId and flips sent=false immediately before re-sending each id-scoped subscription (accounts.subscribe, notifications.subscribe, runtime.clientEvents.subscribe, files.watch). If close() raced that window, closeSharedControlLogicalSubscription found no cleanup id and no sent flag, so it finished the record locally without sending an *.unsubscribe — leaking the server subscription the in-flight resubscribe was about to create. Fix: a new per-subscription awaitingResubscribe flag arms closeAfterReady during that window, so the deferred close fires the unsubscribe once the fresh server id arrives, then finishes.

  2. Benign snapshot overflow rendered a fatal red banner (fix(remote): treat oversized snapshot skip as benign…). An initial replay snapshot over the 2 MiB limit is skipped while live output keeps flowing — informational, not fatal — but the multiplexer's onError(REMOTE_TERMINAL_SNAPSHOT_TOO_LARGE) propagated through the transport to a red xterm banner. Fix: classify that exact message as benign in handleRemoteTerminalError (same seam as the existing terminal_gone retire path) and swallow it. The constant is now exported so the transport can match on it.

  3. Viewport batcher clear() left pending set (fix(remote): clear pending viewport in batcher clear()…). The text batcher's clear() nulls its pending buffer; the viewport batcher's clear() only cancelled the timer, leaving the queued viewport in place — a later flush() or batcher reuse could emit a stale resize. Fix: null pending in clear(), matching the text batcher.

Screenshots

No visual change.

Testing

  • pnpm typecheck (green)
  • pnpm oxlint on all touched files (clean)
  • Targeted suites (with --config config/vitest.config.ts so @/ aliases resolve):
    • remote-runtime-shared-control-subscriptions.test.ts — NEW: 3 cases (established close cleans up by id; close-during-replay defers then unsubscribes with the fresh id; never-sent close finishes locally with no unsubscribe). The replay-window case fails on pre-fix code (finishes locally, size 0, no unsubscribe).
    • remote-runtime-pty-transport.test.ts — updated the oversized-snapshot case to assert onError is not called and live output continues (44 passed).
    • remote-runtime-pty-batching.test.ts — NEW viewport clear() cases; the flush-after-clear case fails on pre-fix code (9 passed after fix).
    • remote-runtime-shared-control-connection.test.ts — passes (this real-websocket suite is pre-existing flaky under heavy parallel load with runtime_timeout in the request path, unrelated to these changes; passes when run in isolation).

AI Review Report

Reviewed for: correctness of the replay-window race (verified the leak window is remoteSubscriptionId=null + resubscribe in flight, and that awaitingResubscribe is cleared on the successful resubscribe response so normal id-scoped close still works); no double-unsubscribe (established close path unchanged); benign-classification is an exact-string match so it can't mask unrelated errors; viewport clear() change matches the text batcher's existing semantics. Cross-platform: pure TypeScript client logic, no shell/path/shortcut/Electron-platform surface touched — behavior identical on macOS/Linux/Windows. SSH/remote: these are the remote-runtime client paths; the subscription-leak fix directly reduces stranded server subscriptions on flaky-reconnect (SSH/relay) links.

Security Audit

No new input handling, command execution, path handling, auth, secrets, dependency, or IPC surface. The subscription fix sends one additional *.unsubscribe (already an existing, authenticated cleanup message) in a previously-leaking path; the benign-classification fix only suppresses a UI banner and never widens what is treated as success. No follow-up needed.

Notes

Each fix is a separate commit; all three are small and self-contained, so they ship as one PR per the task scope. Do not merge — flagging for review.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The changes clear queued viewport data during batcher teardown, classify oversized remote terminal snapshots as informational, and preserve live output without surfacing an error. Shared control subscriptions now track replay resubscription state, defer close cleanup while a new server identifier is pending, and complete cleanup once that identifier arrives. Tests cover viewport clearing, snapshot handling, and established, replaying, and never-sent subscriptions.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the three main remote-terminal fixes in the PR.
Description check ✅ Passed The description follows the required template and includes the requested summary, testing, review, security, and notes sections.
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.

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

🧹 Nitpick comments (1)
src/shared/remote-runtime-shared-control-subscriptions.test.ts (1)

34-104: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a test for the error-during-replay-window case.

The three tests cover the happy paths well, but there's no test verifying what happens when the resubscribe response is an error during the replay window with closeAfterReady=true. This is the exact scenario where the awaitingResubscribe clearing issue (flagged in remote-runtime-shared-control-state.ts) would cause a stuck subscription. Adding this test would both document the expected error behavior and guard against regression.

🧪 Suggested test for error response during replay window
   it('finishes locally without an unsubscribe for a never-sent subscription', () => {
     const { subscriptions, subscription } = makeSubscriptions()
     // Brand new: never sent, no server subscription exists.
     const request = vi.fn()
     closeSharedControlLogicalSubscription({ subscriptions, subscription, request })

     expect(request).not.toHaveBeenCalled()
     expect(subscriptions.size).toBe(0)
   })
+
+  it('finishes a deferred close locally when the resubscribe response errors', () => {
+    const { subscriptions, subscription } = makeSubscriptions()
+    subscription.sent = true
+    subscription.remoteSubscriptionId = 'server-sub-1'
+
+    const sent: SharedControlLogicalSubscription<unknown>[] = []
+    replaySharedControlSubscriptions({
+      subscriptions,
+      send: (s) => { sent.push(s) },
+      tagReplayedResponses: true
+    })
+
+    const request = vi.fn()
+    closeSharedControlLogicalSubscription({ subscriptions, subscription, request })
+    expect(subscriptions.size).toBe(1)
+    expect(request).not.toHaveBeenCalled()
+
+    // Resubscribe fails — no server subscription was created.
+    handleSharedControlLogicalResponse({
+      subscriptions,
+      subscription,
+      response: { ok: false, id: 'req-1', error: { code: -1, message: 'fail' } } as unknown as RuntimeRpcResponse<unknown>,
+      request
+    })
+
+    // No server subscription to unsubscribe from; deferred close finishes locally.
+    expect(request).not.toHaveBeenCalled()
+    expect(subscriptions.size).toBe(0)
+  })
 })

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 253fdfa5-555e-430c-afbd-49008a98e737

📥 Commits

Reviewing files that changed from the base of the PR and between 2622419 and e00521e.

📒 Files selected for processing (9)
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-batching.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-batching.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts
  • src/renderer/src/runtime/remote-runtime-terminal-multiplexer.ts
  • src/shared/remote-runtime-shared-control-state.ts
  • src/shared/remote-runtime-shared-control-subscriptions.test.ts
  • src/shared/remote-runtime-shared-control-subscriptions.ts
  • src/shared/remote-runtime-shared-control-types.ts

@Jinwoo-H

Copy link
Copy Markdown
Contributor Author

Superseded by #8141, which combines all ten remote-reliability audit fixes, incorporates the valid CodeRabbit follow-ups, and has full CI plus live Electron/SSH validation. Closing this standalone PR so #8141 is the single review target; this fix remains included there.

@Jinwoo-H Jinwoo-H closed this Jul 10, 2026
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