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 {