Flush accepted tab moves before replacement restoration

This commit is contained in:
2026-11-08 01:12:01 +00:00
parent 1ed58126da
commit eadbc03efb
3 changed files with 69 additions and 27 deletions
+8 -12
View File
@@ -67,18 +67,14 @@ class TabLocations {
entry.key: durableTabRoute(entry.value), entry.key: durableTabRoute(entry.value),
}, },
); );
_writes = _writes.then((_) async { // Queue the accepted snapshot immediately, before a replacement owner's
if (_disposed) return; // load can enter the same key's order. Disposal prevents new records, but
try { // must not discard the last valid move already accepted from the user.
final saved = await orderedLocalStorage<bool?>(_storageKey, () async { _writes = orderedLocalStorage(_storageKey, () => store.save(snapshot))
if (_disposed) return null; .then<void>(
return store.save(snapshot); (saved) => persistenceFailed = !saved,
}); onError: (Object _, StackTrace _) => persistenceFailed = true,
if (saved != null) persistenceFailed = !saved; );
} catch (_) {
persistenceFailed = true;
}
});
} }
void dispose() => _disposed = true; void dispose() => _disposed = true;
+15 -10
View File
@@ -40,14 +40,19 @@ publication are still required; this lane did not deploy or restart anything.
## Replacement-owner write ordering ## Replacement-owner write ordering
A disposed owner skips writes that have not started, including operations queued Accepted navigation snapshots enter the shared per-key operation order
behind another storage owner. Already-started operations share a small per-key immediately. Disposal rejects new records and late read-back writes, but does
read/write order with replacement owners; replacement reads wait for an old not discard a valid last move that was already queued. Replacement reads wait
in-flight write before merging and saving newer navigation. Idle operation keys for all previously accepted writes before merging and saving newer navigation.
are removed, and a failure never poisons later operations. This preserves the last move when an app is recreated without a new choice,
while newer replacement navigation still wins. 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 21 focused controller/full-app navigation/ordering cases passed after the final
(`/tmp/memwatch/out.hBbm4Y`), including a blocked old write plus a queued old write, follow-up (`/tmp/memwatch/out.ksAW8x`); analyzer clean (`JMtEk8`). Regressions
disposal, replacement navigation and delayed release. The newer state wins for cover a blocked old write plus a queued accepted old write, disposal, replacement
both an in-memory store and distinct SharedPreferences store instances sharing navigation and delayed release. Older accepted history is written before the
a logical key. Two independent ordering-helper tests passed (`out.FffOAC`). replacement loads it; newer navigation wins afterward. Recreating without a new
choice restores the old owner's last accepted chat, not its previous Settings
location. Both same-instance fake stores and distinct SharedPreferences store
instances sharing one logical key are covered.
+46 -5
View File
@@ -261,7 +261,7 @@ void main() {
}, },
); );
test( test(
'disposed queued writes cannot overwrite a replacement controller', 'accepted old writes finish before a replacement load and newer move',
() async { () async {
final gate = Completer<bool>(); final gate = Completer<bool>();
final store = MemoryTabLocationsStore()..write = gate; final store = MemoryTabLocationsStore()..write = gate;
@@ -288,10 +288,12 @@ void main() {
expect(store.value.routes[AppDestination.pipelines], AppRoute.settings); expect(store.value.routes[AppDestination.pipelines], AppRoute.settings);
expect( expect(
store.value.routes[AppDestination.chat], store.value.routes[AppDestination.chat],
isNull, AppRoute.chat,
reason: 'the disposed owner never starts its queued Chat write', reason: 'the accepted queued Chat move is durable before replacement',
); );
expect(store.writes, hasLength(2)); expect(store.writes, hasLength(3));
expect(store.writes[1].active, AppDestination.chat);
expect(store.writes.last.active, AppDestination.runs);
}, },
); );
@@ -322,7 +324,46 @@ void main() {
expect(saved.active, AppDestination.runs); expect(saved.active, AppDestination.runs);
expect(saved.routes[AppDestination.runs], AppRoute.run(18)); expect(saved.routes[AppDestination.runs], AppRoute.run(18));
expect(saved.routes[AppDestination.pipelines], AppRoute.settings); expect(saved.routes[AppDestination.pipelines], AppRoute.settings);
expect(saved.routes[AppDestination.chat], isNull); expect(saved.routes[AppDestination.chat], AppRoute.chat);
},
);
test(
'recreating without a new choice restores the last accepted queued move',
() async {
SharedPreferences.setMockInitialValues({});
final oldStore = GatedPreferencesTabLocationsStore(
scope: 'ordinary-restart',
);
final old = TabLocations(oldStore)..record(AppRoute.settings);
await old.restore(restoreActive: false);
await oldStore.started.future;
old.record(AppRoute.chatSession('last-open-chat'));
old.dispose();
old.record(AppRoute.run(999)); // disposal rejects new, unaccepted moves
const newStore = SharedPreferencesTabLocationsStore(
scope: 'ordinary-restart',
);
final replacement = TabLocations(newStore)
..record(AppRoute.pipelines, visited: false);
var readFinished = false;
final restoring = replacement.restore(restoreActive: true).then((value) {
readFinished = true;
return value;
});
await Future<void>.delayed(Duration.zero);
expect(readFinished, isFalse);
oldStore.gate.complete();
expect(await restoring, AppRoute.chatSession('last-open-chat'));
await old.settled;
await replacement.settled;
final saved = await newStore.load();
expect(saved.active, AppDestination.chat);
expect(
saved.routes[AppDestination.chat],
AppRoute.chatSession('last-open-chat'),
);
expect(saved.routes[AppDestination.runs], isNull);
expect(saved.routes[AppDestination.pipelines], AppRoute.settings);
}, },
); );
} }