From 1b5ebf6834f63882eee80e35f1b0e9eb3fcf869d Mon Sep 17 00:00:00 2001 From: Gianluca Boiano Date: Sun, 13 Sep 2026 17:22:34 +0200 Subject: [PATCH] Honor manual disconnect and re-validate state after waiting Sixth review round: - disconnect(manual: true) joining an in-flight automatic teardown now still marks it manual (and cancels the reconnect timer / stops the background service), so a user-initiated disconnect can no longer be followed by an auto-reconnect scheduled by the teardown it joined. - _shouldAutoReconnect is false once _disposed, so a teardown that outlives disposal cannot arm a timer that calls connect() on a disposed connector. - connect()/connectUsb()/connectTcp() and UsbSerialService.connect() re-check the connection state after awaiting the in-flight teardown. The await is a suspension point, so two callers could previously pass the initial guard, resume together and both run the open sequence. --- analysis_options.yaml | 9 +++++ lib/connector/meshcore_connector.dart | 39 ++++++++++++++++++++- lib/services/usb_serial_service_native.dart | 7 ++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 0d290213..bf8d4218 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -7,6 +7,15 @@ # The following line activates a set of recommended lints for Flutter apps, # packages, and plugins designed to encourage good coding practices. +analyzer: + exclude: + - build/** + - android/** + - ios/** + - web/** + - windows/** + - macos/** + - linux/** include: package:flutter_lints/flutter.yaml linter: diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 714ed0b8..7672fff1 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -1774,6 +1774,16 @@ class MeshCoreConnector extends ChangeNotifier { ); await _awaitActiveDisconnect(); + // The await is a suspension point: another connect may have claimed the + // connector while this one was waiting for the teardown. + if (_state == MeshCoreConnectionState.connecting || + _state == MeshCoreConnectionState.connected) { + _appDebugLogService?.warn( + 'connectUsb ignored: already $_state after disconnect wait', + tag: 'USB', + ); + return; + } await stopScan(); _cancelReconnectTimer(); @@ -1873,6 +1883,16 @@ class MeshCoreConnector extends ChangeNotifier { _appDebugLogService?.info('connectTcp: endpoint=$host:$port', tag: 'TCP'); await _awaitActiveDisconnect(); + // The await is a suspension point: another connect may have claimed the + // connector while this one was waiting for the teardown. + if (_state == MeshCoreConnectionState.connecting || + _state == MeshCoreConnectionState.connected) { + _appDebugLogService?.warn( + 'connectTcp ignored: already $_state after disconnect wait', + tag: 'TCP', + ); + return; + } await stopScan(); _cancelReconnectTimer(); @@ -2023,6 +2043,12 @@ class MeshCoreConnector extends ChangeNotifier { } await _awaitActiveDisconnect(); + // The await is a suspension point: another connect may have claimed the + // connector while this one was waiting for the teardown. + if (_state == MeshCoreConnectionState.connecting || + _state == MeshCoreConnectionState.connected) { + return; + } _activeTransport = MeshCoreTransportType.bluetooth; @@ -2710,6 +2736,7 @@ class MeshCoreConnector extends ChangeNotifier { } bool get _shouldAutoReconnect => + !_disposed && !_manualDisconnect && _lastDeviceId != null && _activeTransport == MeshCoreTransportType.bluetooth; @@ -2765,7 +2792,17 @@ class MeshCoreConnector extends ChangeNotifier { bool skipBleDeviceDisconnect = false, }) { final inFlight = _activeDisconnect; - if (inFlight != null) return inFlight; + if (inFlight != null) { + if (manual) { + // A teardown started as automatic must still honour a later manual + // request, otherwise _manualDisconnect stays false and its tail + // schedules an auto-reconnect the user did not ask for. + _manualDisconnect = true; + _cancelReconnectTimer(); + unawaited(_backgroundService?.stop()); + } + return inFlight; + } final teardown = _disconnectInternal( manual: manual, skipBleDeviceDisconnect: skipBleDeviceDisconnect, diff --git a/lib/services/usb_serial_service_native.dart b/lib/services/usb_serial_service_native.dart index 2d5f3d64..f78210c7 100644 --- a/lib/services/usb_serial_service_native.dart +++ b/lib/services/usb_serial_service_native.dart @@ -144,6 +144,13 @@ class UsbSerialService { } catch (_) { // Failures are already logged by the teardown path. } + + // The await above is a suspension point: another caller may have claimed + // the transport while this one waited. + if (_status == UsbSerialStatus.connected || + _status == UsbSerialStatus.connecting) { + throw StateError('USB serial transport is already active'); + } } _status = UsbSerialStatus.connecting;