mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
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:
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user