Close USB serial port off the UI isolate

close(2) on a tty whose output the peer never drained blocks in the
kernel for the line discipline's closing_wait (measured 30.4s on Linux
against a CDC-ACM device that never reads its bulk OUT endpoint, e.g. a
MeshCore board flashed with a BLE-only companion build). FlSerial's
closePort() is a synchronous FFI call, so the whole app froze for that
window on every failed USB connect and on disconnect.

fl_close() only touches process-global native state, so run it through
Isolate.run() and replicate the Dart-side cleanup closePort() does
(stream close, calloc'd I/O buffers, flh reset). Log when the close
exceeds 1s so the "device is not draining" case is visible in the debug
log.

Verified against the wedged device: inline close blocked the calling
isolate for 30476ms with 0 timer ticks; the isolate close took 30302ms
with 302 ticks, i.e. the main isolate stayed responsive throughout.
This commit is contained in:
Gianluca Boiano committed 2026-09-13 12:30:51 +02:00
1 parent 92e1d15d1c
commit 4a25f718a8
2 files changed
+55 -1

No files matched your search

+54 -1
View File
@@ -1,6 +1,9 @@
import 'dart:async';
import 'dart:ffi';
import 'dart:io';
import 'dart:isolate';
import 'package:ffi/ffi.dart';
import 'package:flserial/flserial.dart';
import 'package:flserial/flserial_exception.dart';
import 'package:flutter/foundation.dart';
@@ -322,11 +325,13 @@ class UsbSerialService {
try {
if (serial?.isOpen() == FlOpenStatus.open) {
serial?.setDTR(false);
serial?.closePort();
}
} catch (_) {
// Ignore errors while closing.
}
if (serial != null) {
await _closePortOffUiIsolate(serial);
}
// Note: we do NOT call free() here; that would globally reset native
// state for all ports. The global reset is done in connect() instead,
// before the next open, which is the safer place to do it.
@@ -362,6 +367,54 @@ class UsbSerialService {
);
}
/// Closes the native port without blocking the Dart UI isolate.
///
/// `close(2)` on a tty whose output has not been drained by the peer blocks
/// in the kernel for the line discipline's `closing_wait` (30s by default —
/// measured 30.4s on Linux with a CDC-ACM device that never reads its bulk
/// OUT endpoint, e.g. a MeshCore board flashed with a BLE-only companion
/// build). `FlSerial.closePort()` is a synchronous FFI call, so running it
/// inline freezes the whole app for that entire window. `fl_close()` only
/// touches process-global native state, so it is safe to run in a helper
/// isolate; the Dart-side cleanup that `closePort()` also performs is
/// replicated here afterwards.
Future<void> _closePortOffUiIsolate(FlSerial serial) async {
final flh = serial.flh;
serial.flh = -1;
if (flh >= 0) {
final startedAt = DateTime.now();
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',
);
}
}
try {
await serial.onSerialData.close();
if (serial.serialReadBuff.address != 0) {
calloc.free(serial.serialReadBuff);
serial.serialReadBuff = Pointer.fromAddress(0);
}
if (serial.serialWriteBuff.address != 0) {
calloc.free(serial.serialWriteBuff);
serial.serialWriteBuff = Pointer.fromAddress(0);
}
} catch (_) {
// Ignore errors while releasing Dart-side resources.
}
}
void dispose() {
// Synchronously close the native port so the SerialThread exits before
// the Dart isolate is torn down (e.g. on hot restart). The async
+1
View File
@@ -43,6 +43,7 @@ dependencies:
git:
url: https://github.com/MeshEnvy/flserial.git
ref: 48216310061efc8d5d217cc18014fc2cb501646e
ffi: ^2.1.3 # calloc.free for flserial's native I/O buffers
provider: ^6.1.5+1
shared_preferences: ^2.2.2
uuid: ^4.3.3