From 043737e2c96b51778c9a7496c40991a2f36e36a9 Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Tue, 19 Aug 2025 09:57:02 +0300 Subject: [PATCH] fix(scope): prevent concurrent modification in dispose() - Create defensive copies of _scopeMap and _disposables - Remove redundant try-catch blocks - Improve memory safety during teardown --- cherrypick/lib/src/scope.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cherrypick/lib/src/scope.dart b/cherrypick/lib/src/scope.dart index ce5bbe4..608f72d 100644 --- a/cherrypick/lib/src/scope.dart +++ b/cherrypick/lib/src/scope.dart @@ -486,16 +486,16 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin { /// await myScope.dispose(); /// ``` Future dispose() async { - // First dispose children scopes - for (final subScope in _scopeMap.values) { + // Create copies to avoid concurrent modification + final scopesCopy = Map.from(_scopeMap); + for (final subScope in scopesCopy.values) { await subScope.dispose(); } _scopeMap.clear(); - // Then dispose own disposables - for (final d in _disposables) { - try { - await d.dispose(); - } catch (_) {} + + final disposablesCopy = Set.from(_disposables); + for (final d in disposablesCopy) { + await d.dispose(); } _disposables.clear(); }