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),
},
);
_writes = _writes.then((_) async {
if (_disposed) return;
try {
final saved = await orderedLocalStorage<bool?>(_storageKey, () async {
if (_disposed) return null;
return store.save(snapshot);
});
if (saved != null) persistenceFailed = !saved;
} catch (_) {
persistenceFailed = true;
}
});
// Queue the accepted snapshot immediately, before a replacement owner's
// load can enter the same key's order. Disposal prevents new records, but
// must not discard the last valid move already accepted from the user.
_writes = orderedLocalStorage(_storageKey, () => store.save(snapshot))
.then<void>(
(saved) => persistenceFailed = !saved,
onError: (Object _, StackTrace _) => persistenceFailed = 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
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.
Accepted navigation snapshots enter the shared per-key operation order
immediately. Disposal rejects new records and late read-back writes, but does
not discard a valid last move that was already queued. Replacement reads wait
for all previously accepted writes before merging and saving newer navigation.
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
(`/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`).
21 focused controller/full-app navigation/ordering cases passed after the final
follow-up (`/tmp/memwatch/out.ksAW8x`); analyzer clean (`JMtEk8`). Regressions
cover a blocked old write plus a queued accepted old write, disposal, replacement
navigation and delayed release. Older accepted history is written before the
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(
'disposed queued writes cannot overwrite a replacement controller',
'accepted old writes finish before a replacement load and newer move',
() async {
final gate = Completer<bool>();
final store = MemoryTabLocationsStore()..write = gate;
@@ -288,10 +288,12 @@ void main() {
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',
AppRoute.chat,
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.routes[AppDestination.runs], AppRoute.run(18));
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);
},
);
}