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.
This commit is contained in:
Gianluca Boiano committed 2026-09-17 23:05:43 +02:00
1 parent fa9e9d18cd
commit 1b5ebf6834
3 files changed
+54 -1

No files matched your search

+9
View File
@@ -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:
+38 -1
View File
@@ -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,
@@ -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;