-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
fix(mobile): images loads sometimes cancel too early #27067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e76aa7
refactor listener tracking for image stream completers and fix early …
LeLunZ 6929bf7
fix: improve cache listener identification in image stream tracking
LeLunZ 3218238
add documentation and test cases for listener tracking in ImageStream…
LeLunZ 1d46b00
fix: remove unnecessary image provision flag from listener tracking
LeLunZ c27f5ca
fix: override setImage method in cache aware listener tracker mixin
LeLunZ fd29a34
fix: rename test file
LeLunZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
mobile/lib/presentation/widgets/images/cache_aware_listener_tracker.mixin.dart
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import 'package:flutter/painting.dart'; | ||
|
|
||
| /// Tracks listeners on an [ImageStreamCompleter] to safely cancel in-flight | ||
| /// network requests without interfering with [ImageCache] internals. | ||
| /// | ||
| /// ### Problem | ||
| /// Cancelling fetches when the listener count drops to 1 (cache only) or 0 | ||
| /// is unsafe due to three framework behaviours: | ||
| /// | ||
| /// 1. **Memory-pressure eviction** — `ImageCache.clear()` removes the cache | ||
| /// listener while UI widgets still need the image. A count-based check | ||
| /// would cancel the active fetch, leaving the UI with no image. | ||
| /// 2. **Synchronous detach during `putIfAbsent`** — When an `initialImage` | ||
| /// is provided, the cache attaches, receives the frame, and detaches | ||
| /// synchronously *before* the UI widget can attach. Count reaches 0 and | ||
| /// would trigger a false cancel. | ||
| /// 3. **Listener misidentification** — After the cache detaches (via 1 or 2), | ||
| /// the next UI listener could be mistaken for the cache listener, causing | ||
| /// incorrect cancellations when that widget is disposed. | ||
| /// | ||
| /// ### Solution: First-Listener Heuristic | ||
| /// The cache is always the first listener attached (via `putIfAbsent`). This | ||
| /// mixin records that identity once and uses it for all subsequent decisions: | ||
| /// | ||
| /// * **Identity locking** — The first listener is assumed to be the cache. | ||
| /// Once identified, `_hasIdentifiedCacheListener` prevents reassignment. | ||
| /// * **Targeted cancellation** — Cancel only when the identified cache | ||
| /// listener is the sole remaining listener and no image has been delivered. | ||
| /// * **Sync-removal bypass** — When `hadInitialImage` is set, the first | ||
| /// synchronous removal of the cache listener is ignored so the fetch | ||
| /// survives until the UI attaches. | ||
| mixin CacheAwareListenerTrackerMixin on ImageStreamCompleter { | ||
|
mertalev marked this conversation as resolved.
|
||
| void Function()? _onLastListenerRemoved; | ||
| int _listenerCount = 0; | ||
| bool _hadInitialImage = false; | ||
| bool _hasIgnoredFirstSyncRemoval = false; | ||
| ImageStreamListener? _cacheListener; | ||
| bool _hasIdentifiedCacheListener = false; | ||
|
|
||
| /// Initializes the tracking state. Must be called in the subclass constructor. | ||
| void setupListenerTracking({required bool hadInitialImage, void Function()? onLastListenerRemoved}) { | ||
| _hadInitialImage = hadInitialImage; | ||
| _onLastListenerRemoved = onLastListenerRemoved; | ||
| } | ||
|
|
||
| @override | ||
| void addListener(ImageStreamListener listener) { | ||
| if (!_hasIdentifiedCacheListener) { | ||
| _hasIdentifiedCacheListener = true; | ||
| _cacheListener = listener; | ||
| } | ||
|
|
||
| _listenerCount++; | ||
| super.addListener(listener); | ||
| } | ||
|
LeLunZ marked this conversation as resolved.
|
||
|
|
||
| @override | ||
| void removeListener(ImageStreamListener listener) { | ||
| super.removeListener(listener); | ||
| _listenerCount--; | ||
|
|
||
| final bool isCacheListener = listener == _cacheListener; | ||
| if (isCacheListener) { | ||
| _cacheListener = null; | ||
| } | ||
|
|
||
| if (_hadInitialImage && !_hasIgnoredFirstSyncRemoval && isCacheListener) { | ||
| _hasIgnoredFirstSyncRemoval = true; | ||
| return; | ||
| } | ||
|
|
||
| final bool onlyCacheListenerLeft = _listenerCount == 1 && _cacheListener != null; | ||
|
|
||
| final bool completelyAbandoned = _listenerCount == 0; | ||
|
|
||
|
LeLunZ marked this conversation as resolved.
|
||
| if (onlyCacheListenerLeft || completelyAbandoned) { | ||
| final onLastListenerRemoved = _onLastListenerRemoved; | ||
| if (onLastListenerRemoved != null) { | ||
| _onLastListenerRemoved = null; | ||
| onLastListenerRemoved(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.