diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 6e23acf7..1c5cfefd 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -444,6 +444,19 @@ class MeshCoreConnector extends ChangeNotifier { bool _contactSyncRetryUsed = false; bool _contactSyncDecisionPending = false; ContactSyncShortfall? _contactSyncShortfall; + // One contact request at a time: the firmware refuses a second + // CMD_GET_CONTACTS mid-stream with ERR_CODE_BAD_STATE. A request made while + // one runs is queued and sent after END. A request with no contact frames + // for [contactRequestStaleAfter] is treated as lost so a missing END cannot + // block every later refresh (#672, owner decision on the 30 s value). + static const Duration contactRequestStaleAfter = Duration(seconds: 30); + bool _contactRequestInFlight = false; + bool _contactStreamStarted = false; + DateTime _contactRequestLastActivity = DateTime.fromMillisecondsSinceEpoch(0); + ({int? since, bool preserveExisting})? _queuedContactRequest; + + @visibleForTesting + DateTime Function() contactSyncClock = DateTime.now; bool _isSyncingQueuedMessages = false; bool _deferQueuedContactMessagesUntilContacts = false; bool _isProcessingDeferredQueuedContactMessages = false; @@ -3123,6 +3136,9 @@ class MeshCoreConnector extends ChangeNotifier { _contactSyncDeliveredKeys.clear(); _contactSyncRetryUsed = false; _contactSyncDecisionPending = false; + _contactRequestInFlight = false; + _contactStreamStarted = false; + _queuedContactRequest = null; _isLoadingContacts = false; _hasLoadedContacts = false; _isLoadingChannels = false; @@ -3684,6 +3700,22 @@ class MeshCoreConnector extends ChangeNotifier { Future getContacts({int? since, bool preserveExisting = false}) async { if (!isConnected) return; + if (_contactRequestInFlight) { + final idle = contactSyncClock().difference(_contactRequestLastActivity); + if (idle < contactRequestStaleAfter) { + _queueContactRequest(since: since, preserveExisting: preserveExisting); + return; + } + _appDebugLogService?.warn( + 'Contact request had no reply for ${idle.inSeconds}s; treating it as ' + 'lost and sending a new one', + tag: 'ContactSync', + ); + } + _contactRequestInFlight = true; + _contactStreamStarted = false; + _contactRequestLastActivity = contactSyncClock(); + _isLoadingContacts = true; _contactSyncTotal = null; _contactSyncReceived = 0; @@ -3694,7 +3726,57 @@ class MeshCoreConnector extends ChangeNotifier { } notifyListeners(); - await sendFrame(buildGetContactsFrame(since: since)); + try { + await sendFrame(buildGetContactsFrame(since: since)); + } catch (e) { + _endContactRequest(); + _appDebugLogService?.error( + 'Contact request could not be sent: $e', + tag: 'ContactSync', + ); + notifyListeners(); + rethrow; + } + } + + /// Keeps at most one request waiting. A full request wins over an + /// incremental one, since it covers it. + void _queueContactRequest({int? since, required bool preserveExisting}) { + final queued = _queuedContactRequest; + final full = since == null || (queued != null && queued.since == null); + _queuedContactRequest = ( + since: full ? null : since, + preserveExisting: preserveExisting && (queued?.preserveExisting ?? true), + ); + _appDebugLogService?.info( + 'Contact request queued behind the one in flight ' + '(${full ? 'full' : 'incremental'})', + tag: 'ContactSync', + ); + } + + void _endContactRequest() { + _contactRequestInFlight = false; + _contactStreamStarted = false; + _isLoadingContacts = false; + _contactSyncIsFull = false; + } + + void _runQueuedContactRequest() { + final queued = _queuedContactRequest; + if (queued == null || _contactRequestInFlight) return; + _queuedContactRequest = null; + unawaited( + getContacts( + since: queued.since, + preserveExisting: queued.preserveExisting, + ).catchError((Object e) { + _appDebugLogService?.error( + 'Queued contact request failed to send: $e', + tag: 'ContactSync', + ); + }), + ); } Future refreshContacts() async { @@ -5669,6 +5751,8 @@ class MeshCoreConnector extends ChangeNotifier { break; case respCodeContactsStart: debugPrint('Got CONTACTS_START'); + _contactStreamStarted = true; + _contactRequestLastActivity = contactSyncClock(); _isLoadingContacts = true; _contactSyncReceived = 0; _contactSyncDeliveredKeys.clear(); @@ -5704,11 +5788,11 @@ class MeshCoreConnector extends ChangeNotifier { case respCodeEndOfContacts: debugPrint('Got END_OF_CONTACTS'); final wasFullSync = _contactSyncIsFull && _isLoadingContacts; - _isLoadingContacts = false; + _endContactRequest(); _hasLoadedContacts = true; _contactSyncUsesSinceFilter = false; - _contactSyncIsFull = false; if (wasFullSync) _finishFullContactSync(); + _runQueuedContactRequest(); unawaited(updateKnownDiscovered()); notifyListeners(); unawaited(_persistContacts()); @@ -5820,6 +5904,7 @@ class MeshCoreConnector extends ChangeNotifier { // An identity import is refused with a generic ERR (illegal argument for a // malformed key), so it claims the frame first. (#578) if (_completeIdentityImport(IdentityTransfer.rejected)) return; + final caplogWasAwaitingStart = _caplogAwaitingStart; // A caplog download awaiting its START frame: the firmware answers the // generic RESP_CODE_ERR when another stream (block-list / contacts / // observer config) is already in flight. Fail the download fast with a @@ -5868,6 +5953,25 @@ class MeshCoreConnector extends ChangeNotifier { return; } + // A contact request the radio refused before streaming (e.g. BAD_STATE + // while another stream runs). Only claimed when nothing else could own + // the ERR. The saved list is untouched; the request just ends. (#672) + if (_contactRequestInFlight && + !_contactStreamStarted && + !caplogWasAwaitingStart && + _pendingGenericAckQueue.isEmpty) { + _endContactRequest(); + _hasLoadedContacts = true; + _appDebugLogService?.error( + 'Radio refused the contact request (error $errCode); saved contacts ' + 'kept', + tag: 'ContactSync', + ); + notifyListeners(); + _runQueuedContactRequest(); + return; + } + if (_pendingGenericAckQueue.isEmpty) { return; } @@ -6474,6 +6578,7 @@ class MeshCoreConnector extends ChangeNotifier { // push rather than in the contact stream. if (_isLoadingContacts) { _contactSyncDeliveredKeys.add(contactTmp.publicKeyHex); + _contactRequestLastActivity = contactSyncClock(); } if (listEquals(contactTmp.publicKey, _selfPublicKey)) { appLogger.info( diff --git a/test/connector/contact_sync_test.dart b/test/connector/contact_sync_test.dart index d9cabde6..f77480df 100644 --- a/test/connector/contact_sync_test.dart +++ b/test/connector/contact_sync_test.dart @@ -218,6 +218,94 @@ void main() { expect(names(), {'Alpha', 'Echo'}); }); + group('one contact request at a time (#672)', () { + late DateTime now; + + setUp(() { + now = DateTime.utc(2026, 9, 23, 6); + connector.contactSyncClock = () => now; + }); + + test('a request during a stream is queued, not sent, list kept', () async { + seedSaved(); + await connector.getContacts(); + connector.handleFrameForTest(_start(3)); + connector.handleFrameForTest(_contactFrame(0x11, 'Alpha')); + + await connector.getContacts(); + + expect(contactRequests(), 1); + expect(names(), {'Alpha', 'Bravo', 'Charlie'}); + }); + + test('the queued request runs after END', () async { + await connector.getContacts(); + connector.handleFrameForTest(_start(0)); + await connector.getContacts(since: 5, preserveExisting: true); + connector.handleFrameForTest(_end); + await Future.delayed(Duration.zero); + + expect(contactRequests(), 2); + expect(sent.last.length, 5, reason: 'the queued incremental request'); + }); + + test('a queued full request wins over an incremental one', () async { + await connector.getContacts(); + connector.handleFrameForTest(_start(0)); + await connector.getContacts(since: 5, preserveExisting: true); + await connector.getContacts(); + connector.handleFrameForTest(_end); + await Future.delayed(Duration.zero); + + expect(contactRequests(), 2); + expect(sent.last.length, 1, reason: 'a full request has no since'); + }); + + test('ERR before the stream ends the request and keeps the list', () async { + seedSaved(); + await connector.getContacts(); + connector.handleFrameForTest(Uint8List.fromList([respCodeErr, 4])); + + expect(connector.isLoadingContacts, isFalse); + expect(names(), {'Alpha', 'Bravo', 'Charlie'}); + await connector.getContacts(); + expect(contactRequests(), 2, reason: 'no longer blocked'); + }); + + test('a request idle for 30 s is treated as lost', () async { + await connector.getContacts(); + connector.handleFrameForTest(_start(3)); + + now = now.add(const Duration(seconds: 29)); + await connector.getContacts(); + expect(contactRequests(), 1, reason: 'still live at 29 s'); + + now = now.add(const Duration(seconds: 2)); + await connector.getContacts(); + expect(contactRequests(), 2, reason: 'stale at 31 s'); + }); + + test('contact frames keep a long stream alive', () async { + await connector.getContacts(); + connector.handleFrameForTest(_start(3)); + now = now.add(const Duration(seconds: 25)); + connector.handleFrameForTest(_contactFrame(0x11, 'Alpha')); + now = now.add(const Duration(seconds: 25)); + + await connector.getContacts(); + expect(contactRequests(), 1); + }); + + test('a failed send does not leave refreshes blocked', () async { + connector.sendFrameOverrideForTest = (_) => throw Exception('usb gone'); + await expectLater(connector.getContacts(), throwsException); + + connector.sendFrameOverrideForTest = sent.add; + await connector.getContacts(); + expect(contactRequests(), 1); + }); + }); + test('an incremental sync never removes anything', () async { seedSaved(); await connector.getContacts(since: 1, preserveExisting: true);