fix(#1163): review fixes for the misfile epic

From the adversarial review of this branch:

- A disconnect while a write waited on its re-read left the wait to its
  30 s guard. Every disconnect passes _resetSyncProgressState, which now
  ends that wait at once.
- The per-radio history delete snapshots what is stored before deleting,
  as #1128 does, and empties the screen first, so a message arriving
  mid-delete survives on screen and on disk.
- The copy cleanup also holds aside the frozen pre-row blob of a key it
  leaves with no rows. Otherwise the load falls back to that blob and
  the copies come straight back.
- A Resync that fails to start is logged instead of dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1163-radio-switch-misfile
diffwireauto 5 days ago
parent bc625458d1
commit 25fa71c212

@ -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<Channel>.of(_channels)) {
markChannelRead(channel.index);
}

@ -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

@ -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},
}),
);

@ -68,6 +68,14 @@ Future<int> 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 = <String, Set<String>>{
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<int> 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;
}

@ -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<void>.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);

@ -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 {

Loading…
Cancel
Save

Powered by TurnKey Linux.