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.
This commit is contained in:
Gianluca Boiano committed 2026-09-13 15:42:34 +02:00
1 parent fb7958a52e
commit fa9e9d18cd
2 files changed
+12 -2

No files matched your search

+8
View File
@@ -194,6 +194,10 @@ class MeshCoreConnector extends ChangeNotifier {
/// waits out the kernel's closing_wait off the UI isolate, so the teardown /// waits out the kernel's closing_wait off the UI isolate, so the teardown
/// can outlive the tap that starts the next connection. /// can outlive the tap that starts the next connection.
Future<void>? _activeDisconnect; Future<void>? _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 MeshCoreUsbManager _usbManager = MeshCoreUsbManager();
final LinuxBlePairingService _linuxBlePairingService = final LinuxBlePairingService _linuxBlePairingService =
LinuxBlePairingService(); LinuxBlePairingService();
@@ -6944,6 +6948,7 @@ class MeshCoreConnector extends ChangeNotifier {
} }
void _setState(MeshCoreConnectionState newState) { void _setState(MeshCoreConnectionState newState) {
if (_disposed) return;
if (_state != newState) { if (_state != newState) {
_state = newState; _state = newState;
notifyListeners(); notifyListeners();
@@ -6951,6 +6956,7 @@ class MeshCoreConnector extends ChangeNotifier {
} }
void markNotifyDirty() { void markNotifyDirty() {
if (_disposed) return;
if (_notifyListenersDirty && _notifyListenersTimer != null) { if (_notifyListenersDirty && _notifyListenersTimer != null) {
return; return;
} }
@@ -6981,11 +6987,13 @@ class MeshCoreConnector extends ChangeNotifier {
@override @override
void notifyListeners() { void notifyListeners() {
if (_disposed) return;
markNotifyDirty(); markNotifyDirty();
} }
@override @override
void dispose() { void dispose() {
_disposed = true;
_scanSubscription?.cancel(); _scanSubscription?.cancel();
_isScanningSubscription?.cancel(); _isScanningSubscription?.cancel();
_connectionSubscription?.cancel(); _connectionSubscription?.cancel();
+4 -2
View File
@@ -527,10 +527,12 @@ class UsbSerialService {
} }
void _handleSerialData(FlSerialEventArgs event) { void _handleSerialData(FlSerialEventArgs event) {
if (_status != UsbSerialStatus.connected) { if (_status == UsbSerialStatus.disconnecting ||
_status == UsbSerialStatus.disconnected) {
// Teardown already invalidated the native handle; a callback queued // Teardown already invalidated the native handle; a callback queued
// before that would make readList() throw and publish a spurious // 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; return;
} }
try { try {