-
Notifications
You must be signed in to change notification settings - Fork 250
fix: crash when KomodoDefiSdk is disposed during periodic fetch #3117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
92295b0
5ba4fb6
58f3252
8bc1e85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,7 @@ class TradingEntitiesBloc implements BlocBase { | |||||||||||||||||||||||||||||||||||||
| List<MyOrder> _myOrders = []; | ||||||||||||||||||||||||||||||||||||||
| List<Swap> _swaps = []; | ||||||||||||||||||||||||||||||||||||||
| Timer? timer; | ||||||||||||||||||||||||||||||||||||||
| bool _closed = false; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
40
|
||||||||||||||||||||||||||||||||||||||
| bool _closed = false; | |
| /// Dispose resources and mark bloc as closed. | |
| void dispose() { | |
| _closed = true; | |
| timer?.cancel(); | |
| _authModeListener?.cancel(); | |
| _myOrdersController.close(); | |
| _swapsController.close(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Bloc Dispose Fails to Cancel Timer
The dispose() method doesn't set _closed = true or cancel the periodic timer. This allows the timer to continue running after the bloc is disposed, bypassing the _closed checks and potentially causing StateError crashes by attempting to access already-disposed resources.
Copilot
AI
Sep 1, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message check is fragile and could miss disposal errors with different wording. Consider checking the error type more specifically or using a more robust method to detect SDK disposal state.
| updateInProgress = true; | |
| await fetch(); | |
| updateInProgress = false; | |
| try { | |
| await fetch(); | |
| } catch (e) { | |
| if (e is StateError && e.message.contains('disposed')) { | |
| // Check SDK disposal state before running fetch | |
| if (_kdfSdk is Disposable && (_kdfSdk as dynamic).isDisposed == true) { | |
| _closed = true; | |
| return; | |
| } | |
| updateInProgress = true; | |
| try { | |
| await fetch(); | |
| } catch (e) { | |
| if (e is StateError) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Stop the timer and mark closed during disposal to prevent leaks and stray ticks
Right now the periodic timer keeps firing after close and holds references. Cancel it when
_closedflips to true and indispose(). Also mark_closedindispose()and close the controllers.Timer? timer; bool _closed = false; @@ @override void dispose() { - _authModeListener?.cancel(); + _closed = true; + timer?.cancel(); + timer = null; + _authModeListener?.cancel(); + // Best-effort; returns Future<void>, but dispose() is sync. + _myOrdersController.close(); + _swapsController.close(); } @@ timer = Timer.periodic(const Duration(seconds: 1), (_) async { - if (_closed) return; + if (_closed) { + timer?.cancel(); + timer = null; + return; + } @@ - } catch (e) { - if (e is StateError && e.message.contains('disposed')) { - _closed = true; - } else { + } catch (e) { + if (e is StateError && e.message.contains('disposed')) { + _closed = true; + timer?.cancel(); + timer = null; + return; + } else { await log( 'fetch error: $e', path: 'TradingEntitiesBloc.fetch', ); } } finally {Also applies to: 72-75, 81-81, 89-91
🤖 Prompt for AI Agents