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;