Skip to content

fix(mobile): release streams whose ready arrives after a replayed cancel - #22945

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/ready-subscription-id-replay
Sep 28, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/ready-subscription-id-replay

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​100 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​100
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​2 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​2

ELI5

Some live feeds the phone opens on the desktop, like the browser tab preview and the workspace change feed, get a ticket number from the desktop once they start. The phone uses that number to say "stop" later. When the connection drops and comes back, the phone reopens those feeds and the desktop hands out new numbers, but the phone kept the old ones. If you left the screen in the short moment before the new number arrived, the phone said "stop" with the old number, which the desktop no longer recognises, so the new feed kept running until the connection closed again.

What Changed

Before: after a reconnect, leaving a browser tab (or any screen that watches workspace changes) just as the phone was reopening its feeds could leave the desktop streaming screencast frames or workspace events to a phone that was no longer listening. For a browser tab the phone was driving, the desktop could also keep showing that tab as controlled by the phone. That lasted until the connection closed, which on a healthy connection can be a long time, or, for the browser preview, until the phone opened another one.

After: the stop goes to the feed that is actually running. The desktop tears the feed down as soon as the reopened feed reports in.

Mechanism: the direct connection's stream registry (mobile/src/transport/rpc-client-stream-registry.ts) keeps, for each open feed, the id the desktop assigned it in its ready reply. When the socket closes, the registry marks every feed to be reopened on the next connection. That step now also forgets each feed's desktop-assigned id, because the id named a registration on the socket that just closed.

With no id on record, a disposal after the reopen but before the new ready takes the path that already existed for "left before the feed was ready". The feed stays as a cancelled placeholder, and when the new ready arrives the registry sends the unsubscribe with the new id and removes the placeholder. A disposal while still disconnected removes the feed outright, since nothing was sent on the new connection.

Why

The existing cancel-before-ready handling was already correct; it just never ran after a reconnect, because a leftover id from the old socket made the stream look ready. Clearing the id restores the one rule the registry already relies on: "has an id" means "ready on this connection".

The alternative was to keep the old id and add a second check at disposal time, asking whether the id belongs to the current connection. That would put two sources of truth side by side and still need the cancel-before-ready path for the new id. Tracking already-removed requests so a late ready could still be released was also considered. With the id cleared, no path removes a feed that was sent on the current connection and is still waiting for its ready, so that tracking would be dead code.

The relay connection does not have this problem. It never reopens feeds within a session: when a relay session closes, its feed list is cleared (mobile-relay-rpc-session.ts close/fail → streams.clear()), and a new session opens fresh feeds. Moving a connection between direct and relay is not affected either: it opens a new feed on the new session and closes the old one on the old session, and each keeps its own id.

A reopened feed is resent under the same request number on the new connection rather than as a fresh request, so this relies on the connection layer discarding any reply from the closed socket before the registry sees it; it does, because the closed socket's encryption key is cleared before the registry forgets its ids.

Linked Issue

Part of #22928

Visual Proof

Pending: live QA runs after the review loop

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Registry tests (rpc-client-stream-registry.test.ts) run for both browser.screencast and runtime.clientEvents.subscribe:

  • A feed is reopened after a reconnect and disposed before its new ready; then the ready arrives. Exactly one unsubscribe is sent, with the new id, and none with the old one. Fails without the fix.
  • Marking for reopen forgets the id: a disposal while disconnected sends nothing and removes the feed. Fails without the fix.
  • The ordinary "left before the first ready" path is unchanged: one unsubscribe with the id from the late ready.
  • Disposing repeatedly around the late ready still sends exactly one unsubscribe. Fails without the fix.
  • A reopened browser feed replaced by a newer one (another tab) before its ready: the reopened feed is unsubscribed with its new id and the newer feed stays. Fails without the fix.

Client test (rpc-client-runtime-events.test.ts) drives the real direct client through a socket close, reconnect, reauthentication and replay. The disposal before the new ready sends the unsubscribe on the new socket with the new id. Fails without the fix.

Reverting the two-line fix makes 8 of these tests fail; restoring it makes all pass. The mobile typecheck and lint pass on the changed files.

AI Disclosure

Review

  • Security: no change to what the phone may request; it only stops sending an id left over from a closed connection.
  • Cross-platform: phone transport logic only; no platform-specific code.
  • Remote SSH: unaffected; the change is in the phone's connection to the desktop, not in how the desktop reaches its hosts.
  • Mobile: direct connections only; relay connections were already correct because they never reopen feeds within a session.
  • Backwards compatibility (mixed phone/host versions): no wire change. Every desktop version already answers these feeds with a ready carrying an id and accepts an unsubscribe by that id; an older desktop now receives an unsubscribe naming the feed that is actually running, instead of one naming a leftover id.
  • Performance: one fewer pointless unsubscribe after a reconnect, and a feed the phone left no longer keeps streaming until the socket closes.

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

The desktop notification feed has the same stale-id problem, but its id is held by the notification code rather than by the connection, so fixing it means moving that ownership into the connection. That is a separate change in this series.

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 Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: f0600c2f-971b-40c8-b7bf-35fdf1dabc17

📥 Commits

Reviewing files that changed from the base of the PR and between c220d92 and 63900b2.

📒 Files selected for processing (3)
  • mobile/src/transport/rpc-client-runtime-events.test.ts
  • mobile/src/transport/rpc-client-stream-registry.test.ts
  • mobile/src/transport/rpc-client-stream-registry.ts

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


📝 Walkthrough

Walkthrough

The stream registry now clears a stored subscription ID when it marks a stream for replay. Tests cover unsubscribe behavior for runtime client-event and browser screencast subscriptions, including disposal before readiness, while disconnected, and after repeated disposal.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 63900

No actionable merge-blocking risk is evident; the change is ready for normal merge checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 63900

The change is intended to stop a reopened feed when its new registration arrives after cancellation. It does not add a new capability or access path. The mobile cleanup behavior is supported by the code and tests, but desktop-side teardown was not independently verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected cleanup scope is a mobile connection’s browser-screencast or runtime-client-event subscriptions. The inspected change does not add an entrypoint or grant a new desktop capability.

Trust Boundaries and Controls

  • observed — The connection-ready callback initiates replay after authentication. The registry sends unsubscribe using the ID in the ready response, not the ID retained before reconnect.

Resilience and Maintainability Implications

  • observed — A sent, cancelled stream without a ready ID remains until ready supplies its cleanup ID; removal prevents later disposal or duplicate responses from sending another unsubscribe.
🚥 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%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly describes the main fix: releasing streams when a replayed subscription is cancelled before its new ready response.
Description check ✅ Passed The description is complete and relevant. It explains the user impact, mechanism, rationale, scope, testing, compatibility, and checklist status. Visual proof remains pending, but the change is primar…
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

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.

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

✅ No new issues found.

Reviewed changes

  • Replay forgets the closed socket's subscription id — markForReplay() now clears stream.subscriptionId, so a disposal between the replay and the new ready takes the existing cancel-before-ready tombstone path and the late ready's unsubscribe carries the new id.
  • Registry tests parameterized over both replayed stream kinds — browser.screencast and runtime.clientEvents.subscribe pin the new-id unsubscribe, the disconnected disposal, the unchanged first-ready path, and idempotent disposal.
  • End-to-end direct-client test — drives a socket close, reconnect, reauthentication and replay, then asserts the unsubscribe lands on the new socket with the new id.

Verification: the focused suites pass (13/13); reverting only the two-line fix fails exactly 7 of them, including the new end-to-end client test. oxlint on the three changed files and tsc --noEmit in mobile/ are clean. The premise also holds server-side — subscriptions are socket-scoped and torn down on close (runtime-rpc-lifecycle.ts:244), and both browser.screencast.unsubscribe and the connectionId-prefixed runtime.clientEvents.unsubscribe are no-ops for a dead-socket id — so clearing the id is safe rather than a leak.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

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

✅ No new issues found.

Reviewed changes

  • Added a regression test for browser-stream replacement during replay — rpc-client-stream-registry.test.ts now covers the case where a replayed browser.screencast (page-1) is replaced by a newer tab (page-2) before its new ready: the late ready must unsubscribe the id the new connection assigned to page-1 and leave page-2 registered.

The new test exercises a genuine path that the previous suite did not: replaceBrowserStream disposes the pending replayed request, which with the fix routes through the cancel-before-ready tombstone (disposeServerSubscription leaves the sent placeholder in place) and then releases it with the new connection's id. It fails against the reverted two-line fix (unsubscribes page-1-old-connection instead) and passes with it; the focused suites are now 14/14. No production code changed in this commit.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Reviewed and tested. Status: ready for review.

Review. An up-front architecture challenge, a precedent check, readiness checks at the start and the end, and two review rounds, both nits-only. A test was added for a reopened browser stream that is replaced before its ready (7086992); it fails without the fix. Notifications and account feeds are handled in #23032.

Testing. Live QA: an iOS simulator running this branch's phone app, paired to an isolated desktop host. The phone opened a browser tab preview. The desktop host was restarted, so the phone reconnected and replayed its streams, and the phone left the preview immediately. The desktop was not left showing the browser as controlled by or streaming to the phone. The preview opened normally again after the reconnect.

Preview before the restart:
browser-opened.png

Desktop after the restart and an immediate leave (no stuck control or stream):
desktop-after-leave.png

Preview after the reconnect:
preview-after-reconnect.png

@brennanb2025
brennanb2025 merged commit bf91941 into main Sep 28, 2026
22 of 24 checks passed
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