From 0ddc1dd2f80f11135a5e81579f800f0615d9a952 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 01:09:33 +0000 Subject: [PATCH] Protect restored Settings categories from disposed owner writes --- app/lib/features/settings/settings_tabs.dart | 9 ++- .../settings/settings_topic_store.dart | 17 ++-- app/notes/settings-topic-navigation.md | 14 ++++ .../settings/settings_topic_restore_test.dart | 81 +++++++++++++++++++ 4 files changed, 114 insertions(+), 7 deletions(-) diff --git a/app/lib/features/settings/settings_tabs.dart b/app/lib/features/settings/settings_tabs.dart index 2e5fdc30..38771889 100644 --- a/app/lib/features/settings/settings_tabs.dart +++ b/app/lib/features/settings/settings_tabs.dart @@ -43,6 +43,7 @@ class _SettingsTopicTabsState extends State final Set _visited = {SettingsTopic.appearance}; SettingsTopicStore? _store; int _revision = 0; + int _storeGeneration = 0; bool _restoring = false; Future _writes = Future.value(); @override @@ -63,11 +64,15 @@ class _SettingsTopicTabsState extends State } 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 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 (_) { diff --git a/app/lib/features/settings/settings_topic_store.dart b/app/lib/features/settings/settings_topic_store.dart index 3d80ccf1..2f35218a 100644 --- a/app/lib/features/settings/settings_topic_store.dart +++ b/app/lib/features/settings/settings_topic_store.dart @@ -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 load() async { + Future load() => orderedLocalStorage(key, () async { try { return (await SharedPreferences.getInstance()).getString(key); } catch (_) { return null; } - } + }); @override - Future save(String topic) async { + Future 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. } - } + }); } diff --git a/app/notes/settings-topic-navigation.md b/app/notes/settings-topic-navigation.md index 25785baa..80d0e513 100644 --- a/app/notes/settings-topic-navigation.md +++ b/app/notes/settings-topic-navigation.md @@ -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. diff --git a/app/test/features/settings/settings_topic_restore_test.dart b/app/test/features/settings/settings_topic_restore_test.dart index 4878fd2c..9f127714 100644 --- a/app/test/features/settings/settings_topic_restore_test.dart +++ b/app/test/features/settings/settings_topic_restore_test.dart @@ -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(); + final writes = []; + String? value; +} + +class SharedQueuedTopicStore implements SettingsTopicStore { + SharedQueuedTopicStore(this.backend); + final SharedTopicBackend backend; + @override + Future load() => + orderedLocalStorage(backend.key, () async => backend.value); + @override + Future 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(); + 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', + ); + }, + ); }