diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 8cd9824a..7f0ed529 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -4865,6 +4865,11 @@ class MeshCoreConnector extends ChangeNotifier { _queueSyncTimeout = null; _queueSyncRetries = 0; _isSyncingChannels = false; + // A write waiting on its re-read must hear now that the re-read will not + // finish, not after its 30 s guard. Every disconnect passes here (#1163). + final syncDone = _channelSyncDone; + _channelSyncDone = null; + if (syncDone != null && !syncDone.isCompleted) syncDone.complete(false); _channelSyncInFlight = false; _channelSyncTimeout?.cancel(); _channelSyncTimeout = null; @@ -12751,6 +12756,14 @@ class MeshCoreConnector extends ChangeNotifier { if (!isConnected || prefix.isEmpty) { return HistoryWipeRefusal.notConnected; } + // Emptied first: a message arriving during the wipe then lands in a fresh + // window, and the wipe's snapshot leaves its stored copy alone, so it + // stays both on screen and on disk. + _channelMessages.clear(); + _windowPskHex.clear(); + _conversations.clear(); + _loadedConversationKeys.clear(); + notifyListeners(); final int removed; try { removed = await wipeRadioMessageHistory(prefix); @@ -12761,10 +12774,6 @@ class MeshCoreConnector extends ChangeNotifier { ); return HistoryWipeRefusal.failed; } - _channelMessages.clear(); - _windowPskHex.clear(); - _conversations.clear(); - _loadedConversationKeys.clear(); for (final channel in List.of(_channels)) { markChannelRead(channel.index); } diff --git a/lib/helpers/channel_slot_ui.dart b/lib/helpers/channel_slot_ui.dart index 7e28e4ce..7c3c4a95 100644 --- a/lib/helpers/channel_slot_ui.dart +++ b/lib/helpers/channel_slot_ui.dart @@ -7,6 +7,7 @@ import 'package:provider/provider.dart'; import '../connector/meshcore_connector.dart'; import '../l10n/l10n.dart'; import '../models/channel.dart'; +import '../utils/app_logger.dart'; import 'channel_slot_decision.dart'; import 'snack_bar_builder.dart'; @@ -129,7 +130,14 @@ void resyncChannels(BuildContext context, MeshCoreConnector connector) { } return; } - unawaited(connector.getChannels(force: true)); + unawaited( + connector.getChannels(force: true).catchError((Object e) { + appLogger.error( + 'Resync channels failed to start: $e', + tag: 'ChannelSync', + ); + }), + ); } /// Asks what a key change does with [channelName]'s message history: kept diff --git a/lib/storage/cross_radio_copy_cleanup.dart b/lib/storage/cross_radio_copy_cleanup.dart index 99f88549..4c866880 100644 --- a/lib/storage/cross_radio_copy_cleanup.dart +++ b/lib/storage/cross_radio_copy_cleanup.dart @@ -151,12 +151,25 @@ class CrossRadioCopyCleanup { for (final r in decision.copies) { byKey.putIfAbsent(r.storeKey, () => []).add(r.mergeKey); } + var blobsHeld = 0; for (final entry in byKey.entries) { await blobs.moveMessageRows( entry.key, '$heldPrefix${entry.key}', entry.value, ); + // A key left with no rows reads its frozen pre-#920 blob instead, + // which would put the copies straight back on screen. Rows are a + // superset of that blob (the #952 gate), so when every row was a copy + // the blob holds only copies: it is held aside too (review of #1163). + if (await blobs.messageRowCount(entry.key) == 0) { + final blob = await blobs.read(entry.key); + if (blob != null) { + await blobs.write('$heldPrefix${entry.key}', blob); + await blobs.deleteEverywhere(entry.key); + blobsHeld++; + } + } } await blobs.write( doneMarkerKey, @@ -164,6 +177,7 @@ class CrossRadioCopyCleanup { 'at': DateTime.now().toIso8601String(), 'held': decision.copies.length, 'undecided': decision.undecided, + 'blobsHeld': blobsHeld, 'keys': {for (final e in byKey.entries) e.key: e.value.length}, }), ); diff --git a/lib/storage/radio_data_wipe.dart b/lib/storage/radio_data_wipe.dart index 7dbede05..fbe8d023 100644 --- a/lib/storage/radio_data_wipe.dart +++ b/lib/storage/radio_data_wipe.dart @@ -68,6 +68,14 @@ Future wipeRadioMessageHistory(String radioPrefix) async { final blobs = BlobStore.instance; final prefixes = ['channel_messages_$radioPrefix', 'messages_$radioPrefix']; + // What exists now is what goes. A message saved while this runs is not in + // the snapshot and survives, the #1128 rule (review of #1163). + final rowSnapshot = >{ + for (final storeKey in await blobs.messageStoreKeys()) + if (prefixes.any(storeKey.startsWith)) + storeKey: await blobs.messageMergeKeys(storeKey), + }; + var removed = 0; for (final prefix in prefixes) { for (final key in await blobs.keysWithPrefix(prefix)) { @@ -75,11 +83,9 @@ Future wipeRadioMessageHistory(String radioPrefix) async { removed++; } } - for (final storeKey in await blobs.messageStoreKeys()) { - if (prefixes.any(storeKey.startsWith)) { - await blobs.deleteMessageRows(storeKey); - removed++; - } + for (final entry in rowSnapshot.entries) { + await blobs.deleteMessageRowsIn(entry.key, entry.value); + removed++; } return removed; } diff --git a/test/connector/channel_write_reread_test.dart b/test/connector/channel_write_reread_test.dart index 48791851..4cc4f3aa 100644 --- a/test/connector/channel_write_reread_test.dart +++ b/test/connector/channel_write_reread_test.dart @@ -73,6 +73,22 @@ void main() { }); }); + test('a disconnect during the re-read ends the wait at once', () async { + // The radio goes quiet: no reply to the write, none to the re-read. + c.sendFrameOverrideForTest = (_) {}; + final write = c.setChannel(3, 'Weather', _psk(0x22)); + await Future.delayed(const Duration(milliseconds: 60)); + expect(c.isSyncingChannels, isTrue, reason: 'the re-read is running'); + + await c.disconnect(manual: true); + + expect( + await write.timeout(const Duration(seconds: 5)), + ChannelWrite.noReply, + reason: 'before the fix this waited out the 30 s guard', + ); + }); + group('a delete with no reply', () { test('is reported done when the re-read shows the slot empty', () async { final result = await c.deleteChannel(3); diff --git a/test/storage/cross_radio_copy_cleanup_test.dart b/test/storage/cross_radio_copy_cleanup_test.dart index 195fc21d..2141c2f2 100644 --- a/test/storage/cross_radio_copy_cleanup_test.dart +++ b/test/storage/cross_radio_copy_cleanup_test.dart @@ -145,6 +145,30 @@ void main() { }, ); + test('a key left with no rows has its old blob held aside too, so the ' + 'copies cannot come back from it', () async { + await seedFieldPattern(); + // B's offband history from before rows (#920): the same copy, frozen. + await blobs.write( + _key(_b, 'offband'), + jsonEncode([ + {'text': 'alert', 'rxTime': _t0 + _min}, + ]), + ); + + await CrossRadioCopyCleanup.run(blobs); + + expect(await blobs.read(_key(_b, 'offband')), isNull); + expect( + await blobs.read( + '${CrossRadioCopyCleanup.heldPrefix}${_key(_b, 'offband')}', + ), + isNotNull, + reason: 'held, not deleted', + ); + expect(await blobs.messageMergeKeys(_key(_a, 'claude')), {'alert'}); + }); + test( 'runs once: a later launch does not touch the history again', () async {