fix(host-api): tear down subscriptions on transport disposal#196
Merged
Conversation
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
handleSubscription tracked each producer's unsubscribe per requestId but only ran them via the cleanup it returned. transport.destroy() never called that cleanup, and host-container's subscription slot discards it — so a disposed transport left producers running. Their next (often batched/async) emission called send() -> postMessage() -> throwIfDisposed(), throwing an uncaught 'Transport is disposed' (seen in Sentry on product close). - Register the teardown via transport.onDestroy so destroy() drains every active producer subscription, regardless of what the caller does with the returned cleanup. - Guard the receive/interrupt callbacks with the disposed flag so an emission already queued at disposal is dropped silently instead of throwing. - stop handler now deletes its map entry so destroy() can't double-unsub a subscription that was already stopped.
Contributor
Author
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. 🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
johnthecat
requested changes
Jun 2, 2026
johnthecat
approved these changes
Jun 3, 2026
cuteWarmFrog
added a commit
that referenced
this pull request
Jun 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes the uncaught
Error: Transport is disposedthrown from a subscription producer after a transport is destroyed (seen in Sentry when a product/container is closed — e.g. statement-store flush after navigating away).handleSubscriptiontracks each producer'sunsubscribeperrequestId, but only runs them via the cleanup it returns.transport.destroy()never calls that cleanup, andhost-container's subscription slot discards it. So a disposed transport leaves producers running; their next (often batched/async) emission callssend()→postMessage()→throwIfDisposed()and throws, unhandled.Changes Made
In
packages/host-api/src/transport.ts,handleSubscription:transport.onDestroy(...), sodestroy()tears down every active producer subscription regardless of what the caller does with the returned cleanup. The returned cleanup also unregisters the destroy listener.receive/interruptcallbacks with thedisposedflag, so an emission already queued at the moment of disposal is dropped silently instead of throwing.stophandler now deletes its map entry, so a subscription that was already stopped isn't unsubscribed again whendestroy()drains the map.One place, fixes every consumer (statement-store, theme, accounts, preimage, chat, payments).
Tests
Added two tests in
transport.spec.ts:destroy()runs the producer's unsubscribe.destroy()does not throw and is not delivered.Both fail on the current code and pass with this change.
Verification
nx typecheck host-api: passnx lint host-api: 0 errors (pre-existinganywarnings only)host-api/transport.spec.ts: 9/9 pass🤖 Generated with Claude Code