Coordinate dispose() with the off-isolate close

Review follow-up:

- dispose() now takes ownership of the FlSerial (nulls _serial) and clears
  serialReadBuff/serialWriteBuff after the synchronous closePort(); flserial
  frees those buffers but leaves the pointer fields dangling, so a later
  disconnect() reaching _closePortOffUiIsolate() with the same object would
  have freed them a second time. _closePortOffUiIsolate() additionally
  returns early when flh is already -1.
- dispose() skips its synchronous close when a close is already in flight:
  fl_close() sets breakThread and joins the SerialThread before it reaches
  the blocking ::close(), so the thread — and its NativeCallable — is
  already gone while the helper isolate waits out closing_wait. The
  in-flight future is tracked in _pendingNativeClose.
- If Isolate.run itself fails the port is now closed inline rather than
  leaking a native slot and SerialThread with no reachable handle.
This commit is contained in:
Gianluca Boiano committed 2026-09-13 12:50:06 +02:00
1 parent 4a25f718a8
commit 9a90d1f448
1 file changed
+54 -21
+54 -21
View File
@@ -35,6 +35,8 @@ class UsbSerialService {
String? _connectedPortKey;
String? _connectedPortLabel;
FlSerial? _serial;
/// Non-null while the native port is being closed in a helper isolate.
Future<void>? _pendingNativeClose;
AppDebugLogService? _debugLogService;
Object? _lastError;
@@ -330,7 +332,15 @@ class UsbSerialService {
// Ignore errors while closing.
}
if (serial != null) {
await _closePortOffUiIsolate(serial);
final pending = _closePortOffUiIsolate(serial);
_pendingNativeClose = pending;
try {
await pending;
} finally {
if (identical(_pendingNativeClose, pending)) {
_pendingNativeClose = null;
}
}
}
// Note: we do NOT call free() here; that would globally reset native
// state for all ports. The global reset is done in connect() instead,
@@ -380,26 +390,38 @@ class UsbSerialService {
/// replicated here afterwards.
Future<void> _closePortOffUiIsolate(FlSerial serial) async {
final flh = serial.flh;
if (flh < 0) {
// Already closed synchronously by dispose(); `closePort()` freed the
// native slot, the stream and both I/O buffers. Touching them again
// would double-free the calloc'd buffers.
return;
}
serial.flh = -1;
if (flh >= 0) {
final startedAt = DateTime.now();
final startedAt = DateTime.now();
try {
await Isolate.run(() => bindings.fl_close(flh));
} catch (error) {
// Spawning the helper failed, so the native slot and its SerialThread
// are still alive with no reachable handle. Close it inline instead —
// that blocks, but only in this already-degraded path.
_debugLogService?.warn(
'Off-isolate USB port close failed ($error); closing inline',
tag: 'USB Serial',
);
try {
await Isolate.run(() => bindings.fl_close(flh));
} catch (error) {
_debugLogService?.warn(
'Native USB port close failed: $error',
tag: 'USB Serial',
);
}
final elapsed = DateTime.now().difference(startedAt);
if (elapsed > const Duration(seconds: 1)) {
_debugLogService?.warn(
'Native USB port close took ${elapsed.inMilliseconds}ms — the device '
'was not draining its serial input',
tag: 'USB Serial',
);
bindings.fl_close(flh);
} catch (_) {
// Ignore errors while closing.
}
}
final elapsed = DateTime.now().difference(startedAt);
if (elapsed > const Duration(seconds: 1)) {
_debugLogService?.warn(
'Native USB port close took ${elapsed.inMilliseconds}ms — the device '
'was not draining its serial input',
tag: 'USB Serial',
);
}
try {
await serial.onSerialData.close();
if (serial.serialReadBuff.address != 0) {
@@ -421,14 +443,25 @@ class UsbSerialService {
// disconnect() path via unawaited() offers no ordering guarantee — the
// isolate may die before the Future resolves, leaving the thread alive
// with a dangling NativeCallable pointer.
if (_useDesktopFlSerial) {
//
// When a close is already in flight there is nothing left to do
// synchronously: `fl_close()` sets `breakThread` and joins the
// SerialThread *before* it reaches the blocking `::close()`, so the
// thread — and with it the NativeCallable — is already gone while the
// helper isolate waits out the kernel's closing_wait.
if (_useDesktopFlSerial && _pendingNativeClose == null) {
final serial = _serial;
_serial = null;
try {
if (serial?.isOpen() == FlOpenStatus.open) {
serial?.setDTR(false);
serial?.closePort(); // synchronous C call — kills the SerialThread
if (serial != null && serial.isOpen() == FlOpenStatus.open) {
serial.setDTR(false);
serial.closePort(); // synchronous C call — kills the SerialThread
}
} catch (_) {}
// closePort() frees both I/O buffers but leaves the pointer fields
// dangling; clear them so nothing can free them a second time.
serial?.serialReadBuff = Pointer.fromAddress(0);
serial?.serialWriteBuff = Pointer.fromAddress(0);
}
// Kick off the full async teardown for anything else (subscription cancel,
// stream controller close). These are best-effort at dispose time.