From 92295b005234c4012a5020b8b11762fa93bd1d13 Mon Sep 17 00:00:00 2001 From: DeckerSU Date: Fri, 29 Aug 2025 22:35:03 +0200 Subject: [PATCH] fix crash when KomodoDefiSdk is disposed during periodic fetch Added a `_closed` flag to TradingEntitiesBloc to prevent periodic updates from running after the SDK has been disposed. The timer callback and `fetch` now check `_closed` before accessing `_kdfSdk`. This prevents `StateError (Bad state: KomodoDefiSdk has been disposed)` from crashing the app after sign-out or SDK disposal. --- lib/blocs/trading_entities_bloc.dart | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/blocs/trading_entities_bloc.dart b/lib/blocs/trading_entities_bloc.dart index 94fcd863e2..48ec3dff41 100644 --- a/lib/blocs/trading_entities_bloc.dart +++ b/lib/blocs/trading_entities_bloc.dart @@ -36,6 +36,7 @@ class TradingEntitiesBloc implements BlocBase { List _myOrders = []; List _swaps = []; Timer? timer; + bool _closed = false; final StreamController> _myOrdersController = StreamController>.broadcast(); @@ -61,6 +62,7 @@ class TradingEntitiesBloc implements BlocBase { } Future fetch() async { + if (_closed) return; if (!await _kdfSdk.auth.isSignedIn()) return; myOrders = await _myOrdersService.getOrders() ?? []; @@ -76,12 +78,25 @@ class TradingEntitiesBloc implements BlocBase { bool updateInProgress = false; timer = Timer.periodic(const Duration(seconds: 1), (_) async { + if (_closed) return; if (updateInProgress) return; // TODO!: do not run for hidden login or HW updateInProgress = true; - await fetch(); - updateInProgress = false; + try { + await fetch(); + } catch (e) { + if (e is StateError && e.message.contains('disposed')) { + _closed = true; + } else { + await log( + 'fetch error: $e', + path: 'TradingEntitiesBloc.fetch', + ); + } + } finally { + updateInProgress = false; + } }); }