Serialize connects against the connector-level teardown

Fourth review round:

- UsbSerialService.connect() now waits for _activeDisconnect before it
  claims the `connecting` status, covering the Android USB-host path too;
  previously the wait sat inside the desktop branch, so an Android
  reconnect could still be overwritten by the old teardown.
- MeshCoreConnector.disconnect() is now a re-entrant-safe wrapper around
  _disconnectInternal() that publishes the teardown as _activeDisconnect,
  and connect()/connectUsb()/connectTcp() join it before starting. The
  transport-level guard alone could not prevent the connector's own
  teardown tail from setting the state back to `disconnected` after a new
  connection was up. Repeat disconnect() calls now join the in-flight
  teardown instead of returning early while it is still running.

flutter analyze clean; flutter test 570 passing, 2 pre-existing failures
in message_url_image_helper_test.dart (identical on a stashed tree);
flutter build linux --debug succeeds.
This commit is contained in:
Gianluca Boiano committed 2026-09-13 15:33:44 +02:00
1 parent 10f557e811
commit fb7958a52e
2 files changed
+67 -19

No files matched your search

+21 -18
View File
@@ -126,6 +126,26 @@ class UsbSerialService {
throw UnsupportedError('USB serial is not supported on this platform.');
}
// A previous disconnect may still be running: on desktop its native close
// waits out the kernel's closing_wait in a helper isolate, and the
// teardown only marks the transport disconnected afterwards. Reopening
// before it finishes would let the old teardown overwrite the new
// connection's state (and, on desktop, race fl_free()/fl_close() over the
// process-global flserial_tab). Wait for the whole teardown — on every
// platform — before claiming the `connecting` state.
final pendingDisconnect = _activeDisconnect;
if (pendingDisconnect != null) {
_debugLogService?.info(
'Waiting for the previous USB disconnect to finish before reopening',
tag: 'USB Serial',
);
try {
await pendingDisconnect;
} catch (_) {
// Failures are already logged by the teardown path.
}
}
_status = UsbSerialStatus.connecting;
var normalizedPortName = normalizeUsbPortName(portName);
_frameDecoder.reset();
@@ -163,24 +183,7 @@ class UsbSerialService {
//
// This must happen before we register any new NativeCallable, so it must
// be the very first thing we do in the desktop branch.
// A previous disconnect may still be running: its native close waits out
// the kernel's closing_wait in a helper isolate, and it only marks the
// transport disconnected afterwards. fl_close() and fl_free() mutate the
// same process-global flserial_tab, so reopening now could race the
// helper, and the old teardown could later overwrite the new connection
// state. Wait for the whole teardown, not just the native close.
final pendingDisconnect = _activeDisconnect;
if (pendingDisconnect != null) {
_debugLogService?.info(
'Waiting for the previous USB disconnect to finish before reopening',
tag: 'USB Serial',
);
try {
await pendingDisconnect;
} catch (_) {
// Failures are already logged by the teardown path.
}
}
// (the previous teardown was already awaited above)
try {
bindings.fl_free();