diff --git a/app/lib/application/tab_locations.dart b/app/lib/application/tab_locations.dart index bdc1ed91..ee0357b5 100644 --- a/app/lib/application/tab_locations.dart +++ b/app/lib/application/tab_locations.dart @@ -1,6 +1,7 @@ import 'dart:async'; import '../shell/destination.dart'; +import '../core/storage/ordered_local_storage.dart'; import 'app_route.dart'; import 'tab_locations_store.dart'; import 'tab_location_routes.dart'; @@ -19,6 +20,10 @@ class TabLocations { bool persistenceFailed = false; Future _writes = Future.value(); + Object get _storageKey => store is SharedPreferencesTabLocationsStore + ? (store as SharedPreferencesTabLocationsStore).key + : store; + void record(AppRoute route, {bool visited = true}) { if (_disposed) return; active = tabForRoute(route); @@ -34,7 +39,7 @@ class TabLocations { Future restore({required bool restoreActive}) async { TabLocationsSnapshot saved; try { - saved = await store.load(); + saved = await orderedLocalStorage(_storageKey, store.load); } catch (_) { // Keep in-memory navigation, but never overwrite unread stored history. persistenceFailed = true; @@ -63,8 +68,13 @@ class TabLocations { }, ); _writes = _writes.then((_) async { + if (_disposed) return; try { - persistenceFailed = !await store.save(snapshot); + final saved = await orderedLocalStorage(_storageKey, () async { + if (_disposed) return null; + return store.save(snapshot); + }); + if (saved != null) persistenceFailed = !saved; } catch (_) { persistenceFailed = true; } diff --git a/app/notes/persistent-tab-locations.md b/app/notes/persistent-tab-locations.md index c386dc5c..97f4281d 100644 --- a/app/notes/persistent-tab-locations.md +++ b/app/notes/persistent-tab-locations.md @@ -37,3 +37,17 @@ and scroll offsets are not serialized by this store. Their existing mounted state remains intact within a session. Settings inner-topic persistence is a separate companion change. Manager-owned integrated browser capture/build and publication are still required; this lane did not deploy or restart anything. + +## Replacement-owner write ordering + +A disposed owner skips writes that have not started, including operations queued +behind another storage owner. Already-started operations share a small per-key +read/write order with replacement owners; replacement reads wait for an old +in-flight write before merging and saving newer navigation. Idle operation keys +are removed, and a failure never poisons later operations. + +18 focused tab/controller/full-app navigation cases passed after this follow-up +(`/tmp/memwatch/out.hBbm4Y`), including a blocked old write plus a queued old write, +disposal, replacement navigation and delayed release. The newer state wins for +both an in-memory store and distinct SharedPreferences store instances sharing +a logical key. Two independent ordering-helper tests passed (`out.FffOAC`). diff --git a/app/test/application/tab_locations_test.dart b/app/test/application/tab_locations_test.dart index bda7dfb1..f6630b12 100644 --- a/app/test/application/tab_locations_test.dart +++ b/app/test/application/tab_locations_test.dart @@ -45,6 +45,19 @@ class FailingTabLocationsStore extends MemoryTabLocationsStore { } } +class GatedPreferencesTabLocationsStore + extends SharedPreferencesTabLocationsStore { + GatedPreferencesTabLocationsStore({required super.scope}); + final gate = Completer(); + final started = Completer(); + @override + Future save(TabLocationsSnapshot snapshot) async { + if (!started.isCompleted) started.complete(); + await gate.future; + return super.save(snapshot); + } +} + void main() { TestWidgetsFlutterBinding.ensureInitialized(); test('Settings and nested pages retain Pipelines ownership', () async { @@ -247,4 +260,69 @@ void main() { expect(store.writes, isEmpty); }, ); + test( + 'disposed queued writes cannot overwrite a replacement controller', + () async { + final gate = Completer(); + final store = MemoryTabLocationsStore()..write = gate; + final old = TabLocations(store)..record(AppRoute.settings); + await old.restore(restoreActive: false); + await Future.delayed(Duration.zero); + expect(store.writes, hasLength(1)); + old.record(AppRoute.chat); // queued behind the blocked Settings write + old.dispose(); + final replacement = TabLocations(store)..record(AppRoute.run(12)); + var readFinished = false; + final restoring = replacement.restore(restoreActive: false).then((value) { + readFinished = true; + return value; + }); + await Future.delayed(Duration.zero); + expect(readFinished, isFalse, reason: 'wait for the old in-flight write'); + gate.complete(true); + await restoring; + await old.settled; + await replacement.settled; + expect(store.value.active, AppDestination.runs); + expect(store.value.routes[AppDestination.runs], AppRoute.run(12)); + expect(store.value.routes[AppDestination.pipelines], AppRoute.settings); + expect( + store.value.routes[AppDestination.chat], + isNull, + reason: 'the disposed owner never starts its queued Chat write', + ); + expect(store.writes, hasLength(2)); + }, + ); + + test( + 'different preference store instances share ordering for the same key', + () async { + SharedPreferences.setMockInitialValues({}); + final oldStore = GatedPreferencesTabLocationsStore(scope: 'same-server'); + final old = TabLocations(oldStore)..record(AppRoute.settings); + await old.restore(restoreActive: false); + await oldStore.started.future; + old.record(AppRoute.chat); + old.dispose(); + const newStore = SharedPreferencesTabLocationsStore(scope: 'same-server'); + final replacement = TabLocations(newStore)..record(AppRoute.run(18)); + var readFinished = false; + final restoring = replacement.restore(restoreActive: false).then((value) { + readFinished = true; + return value; + }); + await Future.delayed(Duration.zero); + expect(readFinished, isFalse); + oldStore.gate.complete(); + await restoring; + await old.settled; + await replacement.settled; + final saved = await newStore.load(); + expect(saved.active, AppDestination.runs); + expect(saved.routes[AppDestination.runs], AppRoute.run(18)); + expect(saved.routes[AppDestination.pipelines], AppRoute.settings); + expect(saved.routes[AppDestination.chat], isNull); + }, + ); }