fix(scope): prevent concurrent modification in dispose()

- Create defensive copies of _scopeMap and _disposables
- Remove redundant try-catch blocks
- Improve memory safety during teardown
This commit is contained in:
Sergey Penkovsky
2025-08-19 09:57:02 +03:00
parent ed65e3c23d
commit 043737e2c9

View File

@@ -486,16 +486,16 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
/// await myScope.dispose(); /// await myScope.dispose();
/// ``` /// ```
Future<void> dispose() async { Future<void> dispose() async {
// First dispose children scopes // Create copies to avoid concurrent modification
for (final subScope in _scopeMap.values) { final scopesCopy = Map<String, Scope>.from(_scopeMap);
for (final subScope in scopesCopy.values) {
await subScope.dispose(); await subScope.dispose();
} }
_scopeMap.clear(); _scopeMap.clear();
// Then dispose own disposables
for (final d in _disposables) { final disposablesCopy = Set<Disposable>.from(_disposables);
try { for (final d in disposablesCopy) {
await d.dispose(); await d.dispose();
} catch (_) {}
} }
_disposables.clear(); _disposables.clear();
} }