Serialize reconnect and teardown around the pending close
Second review round: - connect() awaits _pendingNativeClose before touching flserial's global slot table. fl_close() (helper isolate) and fl_free()/fl_init() mutate the same flserial_tab, so reconnecting during the kernel's closing_wait could double-close the old slot or kill the freshly opened one. - dispose() no longer re-enters disconnect() when a close is in flight; the running disconnect owns the ordering (native close, then subscription cancel), so dispose only chains _closeFrameController onto the pending close. - dispose() frees the I/O buffers through the new _releaseSerialBuffers() on every path, not just after a successful closePort(); with _serial nulled, an error/closed-state dispose used to leak both 8 KiB buffers. - _handleSerialData() ignores callbacks once the status leaves `connected`: a callback queued before flh was invalidated made readList() throw and published a spurious transport error mid-teardown.
This commit is contained in:
1 file changed
+56
-17
@@ -162,6 +162,24 @@ class UsbSerialService {
|
|||||||
//
|
//
|
||||||
// This must happen before we register any new NativeCallable, so it must
|
// This must happen before we register any new NativeCallable, so it must
|
||||||
// be the very first thing we do in the desktop branch.
|
// be the very first thing we do in the desktop branch.
|
||||||
|
// A previous close may still be running in its helper isolate (the
|
||||||
|
// kernel can hold close(2) for closing_wait). fl_close() and fl_free()
|
||||||
|
// mutate the same process-global flserial_tab, so opening — and
|
||||||
|
// especially resetting — the table now would race the helper and could
|
||||||
|
// double-close the old slot or kill the freshly opened one.
|
||||||
|
final pendingClose = _pendingNativeClose;
|
||||||
|
if (pendingClose != null) {
|
||||||
|
_debugLogService?.info(
|
||||||
|
'Waiting for the previous USB port close to finish before reopening',
|
||||||
|
tag: 'USB Serial',
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await pendingClose;
|
||||||
|
} catch (_) {
|
||||||
|
// Failures are already logged by the close path.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bindings.fl_free();
|
bindings.fl_free();
|
||||||
bindings.fl_init(16);
|
bindings.fl_init(16);
|
||||||
@@ -424,6 +442,17 @@ class UsbSerialService {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
await serial.onSerialData.close();
|
await serial.onSerialData.close();
|
||||||
|
} catch (_) {
|
||||||
|
// Ignore errors while releasing Dart-side resources.
|
||||||
|
}
|
||||||
|
_releaseSerialBuffers(serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Frees the two calloc'd I/O buffers `openPort()` allocated and clears the
|
||||||
|
/// pointer fields. `FlSerial.closePort()` frees them too but leaves the
|
||||||
|
/// fields dangling, so clearing is what makes a second release safe.
|
||||||
|
void _releaseSerialBuffers(FlSerial serial) {
|
||||||
|
try {
|
||||||
if (serial.serialReadBuff.address != 0) {
|
if (serial.serialReadBuff.address != 0) {
|
||||||
calloc.free(serial.serialReadBuff);
|
calloc.free(serial.serialReadBuff);
|
||||||
serial.serialReadBuff = Pointer.fromAddress(0);
|
serial.serialReadBuff = Pointer.fromAddress(0);
|
||||||
@@ -443,25 +472,29 @@ class UsbSerialService {
|
|||||||
// disconnect() path via unawaited() offers no ordering guarantee — the
|
// disconnect() path via unawaited() offers no ordering guarantee — the
|
||||||
// isolate may die before the Future resolves, leaving the thread alive
|
// isolate may die before the Future resolves, leaving the thread alive
|
||||||
// with a dangling NativeCallable pointer.
|
// with a dangling NativeCallable pointer.
|
||||||
//
|
final pendingClose = _pendingNativeClose;
|
||||||
// When a close is already in flight there is nothing left to do
|
if (pendingClose != null) {
|
||||||
// synchronously: `fl_close()` sets `breakThread` and joins the
|
// A disconnect is already running and owns the teardown ordering
|
||||||
// SerialThread *before* it reaches the blocking `::close()`, so the
|
// (native close first, subscription cancel after). Re-entering
|
||||||
// thread — and with it the NativeCallable — is already gone while the
|
// disconnect() here would cancel the subscription underneath it, so
|
||||||
// helper isolate waits out the kernel's closing_wait.
|
// only chain the frame-controller cleanup onto that close.
|
||||||
if (_useDesktopFlSerial && _pendingNativeClose == null) {
|
unawaited(pendingClose.whenComplete(_closeFrameController));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_useDesktopFlSerial) {
|
||||||
final serial = _serial;
|
final serial = _serial;
|
||||||
_serial = null;
|
_serial = null;
|
||||||
try {
|
if (serial != null) {
|
||||||
if (serial != null && serial.isOpen() == FlOpenStatus.open) {
|
try {
|
||||||
serial.setDTR(false);
|
if (serial.isOpen() == FlOpenStatus.open) {
|
||||||
serial.closePort(); // synchronous C call — kills the SerialThread
|
serial.setDTR(false);
|
||||||
}
|
serial.closePort(); // synchronous C call — kills the SerialThread
|
||||||
} catch (_) {}
|
}
|
||||||
// closePort() frees both I/O buffers but leaves the pointer fields
|
} catch (_) {}
|
||||||
// dangling; clear them so nothing can free them a second time.
|
// Free (or, after closePort(), just clear) the I/O buffers here:
|
||||||
serial?.serialReadBuff = Pointer.fromAddress(0);
|
// _serial is gone, so the disconnect() below can no longer reach them.
|
||||||
serial?.serialWriteBuff = Pointer.fromAddress(0);
|
_releaseSerialBuffers(serial);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Kick off the full async teardown for anything else (subscription cancel,
|
// Kick off the full async teardown for anything else (subscription cancel,
|
||||||
// stream controller close). These are best-effort at dispose time.
|
// stream controller close). These are best-effort at dispose time.
|
||||||
@@ -469,6 +502,12 @@ class UsbSerialService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _handleSerialData(FlSerialEventArgs event) {
|
void _handleSerialData(FlSerialEventArgs event) {
|
||||||
|
if (_status != UsbSerialStatus.connected) {
|
||||||
|
// Teardown already invalidated the native handle; a callback queued
|
||||||
|
// before that would make readList() throw and publish a spurious
|
||||||
|
// transport error that triggers another disconnect.
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
final bytes = event.serial.readList();
|
final bytes = event.serial.readList();
|
||||||
if (bytes.isNotEmpty) {
|
if (bytes.isNotEmpty) {
|
||||||
|
|||||||
Reference in new issue
Block a user