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

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