Protect restored Settings categories from disposed owner writes

This commit is contained in:
2026-11-08 01:09:33 +00:00
parent eadbc03efb
commit 0ddc1dd2f8
4 changed files with 114 additions and 7 deletions
+8 -1
View File
@@ -43,6 +43,7 @@ class _SettingsTopicTabsState extends State<SettingsTopicTabs>
final Set<SettingsTopic> _visited = {SettingsTopic.appearance};
SettingsTopicStore? _store;
int _revision = 0;
int _storeGeneration = 0;
bool _restoring = false;
Future<void> _writes = Future<void>.value();
@override
@@ -63,11 +64,15 @@ class _SettingsTopicTabsState extends State<SettingsTopicTabs>
}
void _bindStore() {
final generation = ++_storeGeneration;
_store =
widget.selectionStore ??
(widget.navigationScope == null
? null
: SharedPreferencesSettingsTopicStore(widget.navigationScope!));
: SharedPreferencesSettingsTopicStore(
widget.navigationScope!,
canSave: () => mounted && generation == _storeGeneration,
));
final revision = ++_revision;
final store = _store;
if (store == null) return;
@@ -102,9 +107,11 @@ class _SettingsTopicTabsState extends State<SettingsTopicTabs>
if (_restoring) return;
++_revision;
final store = _store;
final generation = _storeGeneration;
if (store == null) return;
// Serial writes ensure a slower previous selection cannot win last.
_writes = _writes.then((_) async {
if (!mounted || generation != _storeGeneration) return;
try {
await store.save(next.name);
} catch (_) {
@@ -1,6 +1,7 @@
import 'package:shared_preferences/shared_preferences.dart';
import '../../core/servers/server_scope.dart';
import '../../core/storage/ordered_local_storage.dart';
const settingsTopicKey = 'pipelines.next.settingsTopic.v1';
@@ -11,25 +12,29 @@ abstract interface class SettingsTopicStore {
/// Stores only the selected category, never unsaved fields or popup state.
class SharedPreferencesSettingsTopicStore implements SettingsTopicStore {
const SharedPreferencesSettingsTopicStore(this.scope);
const SharedPreferencesSettingsTopicStore(this.scope, {this.canSave});
final String scope;
final bool Function()? canSave;
String get key => scopedServerKey(settingsTopicKey, scope);
@override
Future<String?> load() async {
Future<String?> load() => orderedLocalStorage(key, () async {
try {
return (await SharedPreferences.getInstance()).getString(key);
} catch (_) {
return null;
}
}
});
@override
Future<void> save(String topic) async {
Future<void> save(String topic) => orderedLocalStorage(key, () async {
if (canSave?.call() == false) return;
try {
await (await SharedPreferences.getInstance()).setString(key, topic);
final preferences = await SharedPreferences.getInstance();
if (canSave?.call() == false) return;
await preferences.setString(key, topic);
} catch (_) {
// The mounted category remains usable when local storage is unavailable.
}
}
});
}
+14
View File
@@ -19,3 +19,17 @@ write ordering. Existing retained-page draft/scroll tests also pass. Analyzer
clean (`flutter-analyze.q5b4VU`). Manager owns the full-app integration wiring,
rendered tab-switch/relaunch capture and delivery. No deployment, server
restart or device testing was performed in this lane.
Replacement ownership is also guarded: a disposed widget or changed scope skips
queued saves before they start, and the production store rechecks its owner
inside the shared per-key storage queue. A replacement Settings page reads after
any already-started write from the previous owner, then saves its newer choice
last. The queue is shared with tab-location persistence through
`core/storage/ordered_local_storage.dart` (companion prerequisite `f6e1ede6`).
The follow-up passed 27 tests including the shared queue tests
(`/tmp/memwatch/out.a86IS6`) and clean analysis (`2CMlDH`). The new recreation
regression blocks old choice A, queues old choice B, disposes the widget,
chooses C in its replacement, then releases A: B never writes and C remains
selected/persisted. A production-store test also verifies reads wait for pending
writes and an invalidated queued save is skipped inside the operation.
@@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/servers/server_scope.dart';
import 'package:pipelines_app/core/storage/ordered_local_storage.dart';
import 'package:pipelines_app/features/settings/settings_tabs.dart';
import 'package:pipelines_app/features/settings/settings_topic_store.dart';
import 'package:shared_preferences/shared_preferences.dart';
@@ -46,6 +47,27 @@ class DelayedStore implements SettingsTopicStore {
}
}
class SharedTopicBackend {
final key = Object();
final firstWrite = Completer<void>();
final writes = <String>[];
String? value;
}
class SharedQueuedTopicStore implements SettingsTopicStore {
SharedQueuedTopicStore(this.backend);
final SharedTopicBackend backend;
@override
Future<String?> load() =>
orderedLocalStorage(backend.key, () async => backend.value);
@override
Future<void> save(String topic) => orderedLocalStorage(backend.key, () async {
backend.writes.add(topic);
if (backend.writes.length == 1) await backend.firstWrite.future;
backend.value = topic;
});
}
void main() {
for (final cupertino in [false, true]) {
testWidgets('restores selected category after recreation ($cupertino)', (
@@ -129,4 +151,63 @@ void main() {
await tester.pumpAndSettle();
expect(find.text('notifications page'), findsOneWidget);
});
testWidgets('recreated owner wins over disposed owner queued selections', (
tester,
) async {
final backend = SharedTopicBackend();
await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend)));
await tester.pumpAndSettle();
await selectSettingsTopic(tester, 'Connection');
expect(backend.writes, ['connection']);
await selectSettingsTopic(tester, 'Notifications');
expect(backend.writes, [
'connection',
], reason: 'old second choice waits behind first write');
await tester.pumpWidget(const SizedBox.shrink());
await tester.pumpWidget(host(store: SharedQueuedTopicStore(backend)));
// Restoration waits for the old write, while this new owner remains usable.
await selectSettingsTopic(tester, 'Tools');
backend.firstWrite.complete();
await tester.pumpAndSettle();
expect(find.text('tools page'), findsOneWidget);
expect(backend.value, 'tools');
expect(backend.writes, ['connection', 'tools']);
});
testWidgets(
'production reads await pending writes and disposed saves are skipped',
(tester) async {
SharedPreferences.setMockInitialValues({});
var current = true;
final store = SharedPreferencesSettingsTopicStore(
'ownership-check',
canSave: () => current,
);
final gate = Completer<void>();
final oldWrite = orderedLocalStorage(store.key, () async {
await gate.future;
await (await SharedPreferences.getInstance()).setString(
store.key,
'connection',
);
});
var readFinished = false;
final read = store.load().then((value) {
readFinished = true;
return value;
});
final staleQueuedSave = store.save('notifications');
await tester.pump();
expect(readFinished, isFalse);
current = false;
gate.complete();
await oldWrite;
expect(await read, 'connection');
await staleQueuedSave;
expect(
(await SharedPreferences.getInstance()).getString(store.key),
'connection',
);
},
);
}