Skip to content

fix(runtime): keep a superseded lease-only subscribe from releasing live mobile presence - #14993

Closed
brennanb2025 wants to merge 1 commit into
brennanb2025/sta-4510-subscription-ownershipfrom
brennanb2025/sta-4510-lease-presence
Closed

brennanb2025 wants to merge 1 commit into
brennanb2025/sta-4510-subscription-ownershipfrom
brennanb2025/sta-4510-lease-presence

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​75
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​6 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​5

ELI5

A phone that reconnects gets a fresh terminal subscription, but the old one may still be mid-await. When that old handler wakes up and sees "my stream is closed", it releases the phone's presence — except presence is filed under (pty, phone), which the new subscription now uses too. So the dead handler deletes the live one's presence on its way out.

What Changed

One guard: the lease-only branch releases mobile presence only if its registration still owns the subscription.

if (registration.isCurrent()) {
  runtime.handleMobileUnsubscribe(ptyId, clientId)
}

Why

handleMobileUnsubscribe is a keyed inner.delete(clientId) with no refcount (orca-runtime.ts:16108-16119), and both generations share an identical (ptyId, clientId). The resume-after-await at terminal.ts:2977 fires when closed || signal?.aborted — and closed === true is exactly the post-rebind state, because the rebind runs the old generation's cleanup synchronously inside registerSubscriptionCleanup. So the superseded handler reliably deletes the live subscriber's presence.

Distinct from the freeze in the parent PR: output keeps flowing. What is lost is phone-fit and remote-view presence, so the terminal stops auto-fitting to the phone's viewport. Same defect family — a stale generation acting on a shared key — which is why it uses the same ownership handle.

Linked Issue

Refs #14843 (found while investigating; not the reported freeze)

Fixes #

Visual Proof

N/A — no rendered surface changes in this diff. The observable effect is that a reconnected phone keeps its server-side auto-fit ownership.

Testing

terminal-subscribe-lease-presence.test.ts drives a lease-only subscribe, leaves handleMobileSubscribe pending, rebinds the id on a second connection, then resolves the first.

Because both generations share (ptyId, clientId), arguments alone cannot distinguish the rebind's own legitimate release from a stale one — so the test snapshots the call count immediately after the rebind and asserts it does not increase.

Mutation-verified: removing the isCurrent() guard turns it red (expected 2 to be 1).

  • Targeted suites: 22 files / 99 tests pass.

  • pnpm tc exit 0; oxfmt + oxlint clean.

  • I manually tested these changes locally

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

Review

Agent skill upstream boundary

  • Not applicable.

Notes

  • Stacked on fix(runtime): tear down terminal subscriptions only through their owning registration #14992 — merge that first; this branch is based on it and depends on registerOwnedSubscriptionCleanup.
  • Known limitation, deliberately not fixed here: an evicted generation's still-pending handleMobileSubscribe can resolve late and write into the same presence entry (orca-runtime.ts:16069-16077), clobbering the live generation's viewport baseline. It cannot delete presence, so it cannot cause a freeze — value corruption only. Named here so it is not rediscovered as a missed case.
  • Backwards compatibility: server-only, no wire change.
  • Remote/SSH, cross-platform: unaffected; platform-independent server logic.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • 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)

Author

  • X / Twitter: @BrennanKB5

…ive mobile presence

The lease-only terminal.subscribe branch releases mobile presence when its awaited
handleMobileSubscribe resumes to find the stream closed. But `closed` is exactly
the post-rebind state, presence is keyed by (ptyId, clientId) with no refcount, and
both generations share that key — so a superseded handler deleted the replacement
subscriber's presence instead of its own.

Gate the release on the registration still owning the subscription.

Distinct from the STA-4510 freeze this stacks on: output keeps flowing, but the
live subscriber loses phone-fit and remote-view presence.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Consolidating this into #14992 rather than shipping it stacked.

Code review on #14992 found that it introduced SubscriptionRegistration.isCurrent with no production caller, and separately flagged the unguarded handleMobileUnsubscribe at terminal.ts:2979 — which is exactly the bug this PR fixes and exactly the caller that API was designed for. Splitting them meant #14992 shipped a dead API alongside the known half-torn state it was meant to prevent.

The one-line guard and its regression test now live in #14992 (805b172541).

Review verdict on this change was approve, no findings. It also corrected something in this PR's description: the documented "known limitation" about a late handleMobileSubscribe clobbering the replacement's viewport baseline is not accurate — every mobileSubscribers write happens synchronously before the first possible await (orca-runtime.ts:16091 for the lease-only path, which passes no viewport and returns immediately). I verified that and dropped the claim rather than carrying it forward.

Closing as consolidated, not abandoned.

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