fix(sse): scope dispose to per-screen provider instance#2271
Merged
Conversation
SSE subscriptions and HTTP clients were stored in static maps but
Screen.dispose() calls dispose on cloned API providers. Disposing any
screen cancelled every SSE connection app-wide, breaking live streams on
still-mounted screens after navigation, modals, or tab switches.
Make subscription state instance-scoped (matching FirestoreAPIProvider)
and resolve getProvider('sse') from the screen provider map instead of
allocating a throwaway instance on each lookup.
Co-authored-by: Sharjeel Yunus <sharjeelyunus@users.noreply.github.com>
…on handling Updated the APIProviders class to utilize a map for provider resolution, ensuring a fallback to SSEAPIProvider when necessary. Additionally, modified the SSE provider tests to verify cancellation behavior of StreamControllers, ensuring proper resource management during disposal.
This was referenced Jun 11, 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.
Bug and impact
Trigger: A screen with SSE configured in
api_providersis disposed while another screen still has an active SSE subscription — e.g. closing a modal, navigating back, or switching bottom-nav tabs whenreloadView: true(default).Impact:
SSEAPIProvider.dispose()cleared static subscription/client maps shared app-wide. Disposing one screen silently killed every SSE stream, leaving still-mounted screens with stale bindings and no live updates.Root cause
_subscriptionsand_activeClientswerestatic, but eachScreenclones and disposes its ownAPIProviderinstances on unmount.APIProviders.getProvider('sse')allocated a newSSEAPIProvider()on every lookup instead of using the per-screen cloned instance from the provider map.Fix
FirestoreAPIProvider).dispose()now cancels only subscriptions owned by that provider instance.getProvider('sse')returnsproviders['sse']when present so subscribe/dispose use the same instance.Validation
modules/ensemble/test/sse_provider_dispose_test.dartcovering per-instance dispose isolation andgetProvider('sse')map resolution.modules/ensemble:flutter test test/sse_provider_dispose_test.dartDuplicate check
listenForChangesdedup crash; different provider/code path.dispose()tearing down other screens' streams.