fix(firestore): dedupe listenForChanges subscriptions to prevent crash#2257
Open
cursor[bot] wants to merge 1 commit into
Open
fix(firestore): dedupe listenForChanges subscriptions to prevent crash#2257cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
…sed event bus Repeated invokeAPI calls with listenForChanges: true accumulated Firestore snapshot listeners because subscribeToApi always appended to a list. Replace the prior subscription for the same apiName (matching SSE provider behavior) and skip callbacks after provider dispose. Also ignore binding dispatches once the page event bus is closed, which avoids crashes when a snapshot arrives after Page dispose but before Screen cancels API providers. Co-authored-by: Sharjeel Yunus <sharjeelyunus@users.noreply.github.com>
1 task
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 calls the same Firestore API with
listenForChanges: truemore than once (e.g. on tab switches, navigation callbacks, or screen refreshes). Each call adds another snapshot listener without cancelling the previous one.Impact: After ~8–15 Firestore updates, callbacks multiply exponentially, the app slows dramatically, and can crash. Navigating away can also crash with
Bad state: Cannot add new events after calling closewhen a snapshot arrives after the page event bus is destroyed but before the screen cancels API providers.Fixes #2253.
Root cause
FirestoreAPIProvider.subscribeToApi()appended every subscription to aList<StreamSubscription>with no deduplication by API name (unlikeSSEAPIProvider, which replaces existing subscriptions).EventBusbeforeScreen.dispose()cancels Firestore listeners, so late snapshot callbacks attempted to dispatch binding updates on a closed bus.Fix
Map<String, StreamSubscription>keyed byapiNameand cancel/replace any existing subscription before subscribing (mirrors SSE provider behavior).Validation
modules/ensemble/test/firestore_listen_dedup_test.dartcovering subscription replacement and closed event-bus behavior.modules/ensemble:flutter test test/firestore_listen_dedup_test.dartDuplicate check
cursor/critical-bug-remediation-0747): TabController zero-length crash — different code path.unsubscribe-changes): attempted Firestore unsubscribe via singleton +unSubscribeToApi; closed without merge; did not guard event-bus dispose race.cursor/critical-bug-remediation-adc5: TabController fix only.