From fa9e9d18cd08c818d118b39097a435de0f50d323 Mon Sep 17 00:00:00 2001 From: Gianluca Boiano Date: Sun, 13 Sep 2026 15:42:34 +0200 Subject: [PATCH] Suppress connector state publication after dispose Fifth review round: - MeshCoreConnector.dispose() sets _disposed, and _setState(), notifyListeners() and markNotifyDirty() no-op afterwards. A teardown started before disposal can still be running (the USB native close waits out the kernel's closing_wait off the UI isolate), and its tail would otherwise call notifyListeners() on a disposed ChangeNotifier. - _handleSerialData() now only drops callbacks in the teardown states (disconnecting/disconnected) instead of everything that is not `connected`, so data arriving in the `connecting` window is still ingested and buffered by the frame decoder. --- lib/connector/meshcore_connector.dart | 8 ++++++++ lib/services/usb_serial_service_native.dart | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index 25046783..714ed0b8 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -194,6 +194,10 @@ class MeshCoreConnector extends ChangeNotifier { /// waits out the kernel's closing_wait off the UI isolate, so the teardown /// can outlive the tap that starts the next connection. Future? _activeDisconnect; + /// Set by [dispose]. A teardown started before disposal can still be running + /// (the USB native close waits out the kernel's closing_wait off the UI + /// isolate), so state publication is suppressed once the connector is gone. + bool _disposed = false; final MeshCoreUsbManager _usbManager = MeshCoreUsbManager(); final LinuxBlePairingService _linuxBlePairingService = LinuxBlePairingService(); @@ -6944,6 +6948,7 @@ class MeshCoreConnector extends ChangeNotifier { } void _setState(MeshCoreConnectionState newState) { + if (_disposed) return; if (_state != newState) { _state = newState; notifyListeners(); @@ -6951,6 +6956,7 @@ class MeshCoreConnector extends ChangeNotifier { } void markNotifyDirty() { + if (_disposed) return; if (_notifyListenersDirty && _notifyListenersTimer != null) { return; } @@ -6981,11 +6987,13 @@ class MeshCoreConnector extends ChangeNotifier { @override void notifyListeners() { + if (_disposed) return; markNotifyDirty(); } @override void dispose() { + _disposed = true; _scanSubscription?.cancel(); _isScanningSubscription?.cancel(); _connectionSubscription?.cancel(); diff --git a/lib/services/usb_serial_service_native.dart b/lib/services/usb_serial_service_native.dart index 781dfcb9..2d5f3d64 100644 --- a/lib/services/usb_serial_service_native.dart +++ b/lib/services/usb_serial_service_native.dart @@ -527,10 +527,12 @@ class UsbSerialService { } void _handleSerialData(FlSerialEventArgs event) { - if (_status != UsbSerialStatus.connected) { + if (_status == UsbSerialStatus.disconnecting || + _status == UsbSerialStatus.disconnected) { // 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. + // transport error that triggers another disconnect. Data that arrives + // while still `connecting` is kept — the decoder buffers it. return; } try {