💚 End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix incorrect ref race
laurenspriem0359067b9d91
diff --git a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dartindex f8c46c03d4..b046c1d556 100644--- a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart+++ b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart@@ -18,6 +18,7 @@ import "package:photos/events/resume_video_event.dart";import "package:photos/events/retry_failed_image_load_event.dart";import "package:photos/generated/l10n.dart";import "package:photos/models/file/extensions/file_props.dart";+import "package:photos/models/file/file.dart";import "package:photos/models/file/file_type.dart";import "package:photos/models/memories/memory.dart";import "package:photos/service_locator.dart";@@ -77,6 +78,8 @@ class _FullScreenMemoryDataUpdaterStateextends State<FullScreenMemoryDataUpdater> {late ValueNotifier<int> indexNotifier;StreamSubscription<List<ConnectivityResult>>? _connectivitySubscription;+ final _ownedThumbnailRefs = <int, ({EnteFile file, Object token})>{};+ final _pendingThumbnailRefIDs = <int>{};// Seeded from checkConnectivity() before the listener attaches, so a real// offline→online recovery (fire retry) is distinguishable from a WiFi↔// cellular handoff where the old requests are still healthy.@@ -114,25 +117,15 @@ class _FullScreenMemoryDataUpdaterState}if (!_wasConnected) {_wasConnected = true;- // Scoped to the warmed range so we don't touch refs owned by- // widgets outside this route. In-window lookahead items hold two- // of our refs (initial warm + per-index), so the second loop- // releases the second ref.final currentIndex = indexNotifier.value;- final end = _warmedRangeEndExclusive();- for (var i = 0; i < end; i++) {- removePendingGetThumbnailRequestIfAny(widget.memories[i].file);- }- for (var i = 1; i <= _thumbnailLookaheadCap; i++) {- final j = currentIndex + i;- if (j >= widget.memories.length) break;- removePendingGetThumbnailRequestIfAny(widget.memories[j].file);- }+ _releaseOwnedThumbnailRefs();Bus.instance.fire(RetryFailedImageLoadEvent());// Re-kick on a microtask so the event handler runs first and clears// the stale map entries; a synchronous call would re-bump the// refcounts before the cancellation.- scheduleMicrotask(() => _warmThumbnailWindow(currentIndex));+ scheduleMicrotask(() {+ if (mounted) _warmThumbnailWindow(currentIndex);+ });}});}@@ -147,23 +140,69 @@ class _FullScreenMemoryDataUpdaterStatefinal end =(fromIndex + _thumbnailLookaheadCap).clamp(0, widget.memories.length);for (var i = fromIndex; i < end; i++) {- preloadThumbnail(widget.memories[i].file);+ _preloadThumbnailOwned(widget.memories[i].file);}}- // max() so back-nav below initialIndex still covers the initial warm.- int _warmedRangeEndExclusive() {- final maxIndex = max(widget.initialIndex, indexNotifier.value);- return (maxIndex + _thumbnailLookaheadCap).clamp(0, widget.memories.length);+ void _preloadThumbnailOwned(EnteFile file) {+ if (!file.isRemoteFile) {+ preloadThumbnail(file);+ return;+ }+ final uploadedFileID = file.uploadedFileID;+ if (uploadedFileID == null) {+ preloadThumbnail(file);+ return;+ }+ if (_ownedThumbnailRefs.containsKey(uploadedFileID) ||+ _pendingThumbnailRefIDs.contains(uploadedFileID)) {+ return;+ }+ _pendingThumbnailRefIDs.add(uploadedFileID);+ unawaited(_preloadRemoteThumbnailOwned(file, uploadedFileID));+ }++ Future<void> _preloadRemoteThumbnailOwned(+ EnteFile file,+ int uploadedFileID,+ ) async {+ try {+ final request = await preloadThumbnailWithPendingRequestRef(file);+ if (!request.acquiredPendingRequestRef) {+ return;+ }+ if (!mounted) {+ removePendingGetThumbnailRequestIfAny(file);+ return;+ }+ final token = Object();+ _ownedThumbnailRefs[uploadedFileID] = (file: file, token: token);+ unawaited(+ request.pendingRequest.whenComplete(() {+ final ref = _ownedThumbnailRefs[uploadedFileID];+ if (ref?.token == token) {+ _ownedThumbnailRefs.remove(uploadedFileID);+ }+ }),+ );+ } catch (_) {+ // Best-effort warmup; visible widgets perform their own load/error path.+ } finally {+ _pendingThumbnailRefIDs.remove(uploadedFileID);+ }+ }++ void _releaseOwnedThumbnailRefs() {+ for (final ref in _ownedThumbnailRefs.values) {+ removePendingGetThumbnailRequestIfAny(ref.file);+ }+ _ownedThumbnailRefs.clear();}@overridevoid dispose() {_connectivitySubscription?.cancel();- final end = _warmedRangeEndExclusive();- for (var i = 0; i < end; i++) {- removePendingGetThumbnailRequestIfAny(widget.memories[i].file);- }+ _releaseOwnedThumbnailRefs();indexNotifier.dispose();super.dispose();}@@ -185,6 +224,7 @@ class _FullScreenMemoryDataUpdaterStatememories: widget.memories,indexNotifier: indexNotifier,removeCurrentMemory: removeCurrentMemory,+ preloadThumbnail: _preloadThumbnailOwned,child: widget.child,);}@@ -194,11 +234,13 @@ class FullScreenMemoryData extends InheritedWidget {final List<Memory> memories;final ValueNotifier<int> indexNotifier;final VoidCallback removeCurrentMemory;+ final void Function(EnteFile file) preloadThumbnail;const FullScreenMemoryData({required this.memories,required this.indexNotifier,required this.removeCurrentMemory,+ required this.preloadThumbnail,required super.child,super.key,});@@ -540,7 +582,9 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {i++) {final j = index + i;if (j >= inheritedData.memories.length) break;- preloadThumbnail(inheritedData.memories[j].file);+ inheritedData.preloadThumbnail(+ inheritedData.memories[j].file,+ );}for (var i = 1;i <=diff --git a/mobile/apps/photos/lib/utils/thumbnail_util.dart b/mobile/apps/photos/lib/utils/thumbnail_util.dartindex b2428c5853..cc3af890df 100644--- a/mobile/apps/photos/lib/utils/thumbnail_util.dart+++ b/mobile/apps/photos/lib/utils/thumbnail_util.dart@@ -44,6 +44,27 @@ Future<Uint8List?> getThumbnail(EnteFile file) async {}}+Future<({bool acquiredPendingRequestRef, Future<void> pendingRequest})>+ preloadThumbnailWithPendingRequestRef(EnteFile file) async {+ if (!file.isRemoteFile) {+ unawaited(getThumbnailFromLocal(file));+ return (+ acquiredPendingRequestRef: false,+ pendingRequest: Future<void>.value(),+ );+ }+ final request = await _getThumbnailFromServerRequest(file);+ final pendingRequest = request.future.then<void>(+ (_) {},+ onError: (_, __) {},+ );+ unawaited(pendingRequest);+ return (+ acquiredPendingRequestRef: request.acquiredPendingRequestRef,+ pendingRequest: pendingRequest,+ );+}+// Note: This method should only be called for files that have been uploaded// since cachedThumbnailPath depends on the file's uploadedIDFuture<File?> getThumbnailForUploadedFile(EnteFile file) async {@@ -66,11 +87,20 @@ Future<File?> getThumbnailForUploadedFile(EnteFile file) async {}Future<Uint8List> getThumbnailFromServer(EnteFile file) async {+ final request = await _getThumbnailFromServerRequest(file);+ return request.future;+}++Future<({Future<Uint8List> future, bool acquiredPendingRequestRef})>+ _getThumbnailFromServerRequest(EnteFile file) async {final cachedThumbnail = cachedThumbnailPath(file);if (await cachedThumbnail.exists()) {final data = await cachedThumbnail.readAsBytes();ThumbnailInMemoryLruCache.put(file, data);- return data;+ return (+ future: Future<Uint8List>.value(data),+ acquiredPendingRequestRef: false,+ );}// Check if there's already in flight request for fetching thumbnail from the// server@@ -86,10 +116,16 @@ Future<Uint8List> getThumbnailFromServer(EnteFile file) async {}_downloadQueue.add(file.uploadedFileID!);_downloadItem(item);- return item.completer.future;+ return (+ future: item.completer.future,+ acquiredPendingRequestRef: true,+ );} else {_uploadIDToDownloadItem[file.uploadedFileID]!.counter++;- return _uploadIDToDownloadItem[file.uploadedFileID]!.completer.future;+ return (+ future: _uploadIDToDownloadItem[file.uploadedFileID]!.completer.future,+ acquiredPendingRequestRef: true,+ );}}