Skip to content

fix(flutter): prevent ConcurrentModificationError in SignalsMixin#436

Merged
rodydavis merged 1 commit intorodydavis:mainfrom
DavidPluxia:fix/concurrent-modification-error
Dec 19, 2025
Merged

fix(flutter): prevent ConcurrentModificationError in SignalsMixin#436
rodydavis merged 1 commit intorodydavis:mainfrom
DavidPluxia:fix/concurrent-modification-error

Conversation

@DavidPluxia
Copy link
Copy Markdown
Contributor

Summary

Fixes #431 - ConcurrentModificationError when using SignalsMixin.

Problem

HashMap.values returns a lazy Iterable, not a snapshot. During iteration in _setup(), accessing s.target.value can trigger bindSignal()_watch(), which mutates _signals while the iterator is active.

Similarly, clearSignalsAndEffects() iterates lazily over _signals and _effects while dispose()/cleanup calls can mutate these collections.

Solution

Convert lazy iterables to List snapshots before iteration:

// Before (lazy - unsafe)
for (final s in _signals.values.where((e) => e.local != null)) {

// After (snapshot - safe)
final signals = _signals.values.where((e) => e.local != null).toList();
for (final s in signals) {

Changes

  1. _setup(): Snapshot _signals.values before iterating
  2. clearSignalsAndEffects(): Snapshot both _signals.values and _effects before iterating

Minimal, surgical fix with no behavioral changes.

HashMap.values returns a lazy Iterable. During iteration, accessing
s.target.value can trigger bindSignal() which calls _watch(), mutating
_signals while the iterator is active.

Similarly, clearSignalsAndEffects() iterates lazily over _signals and
_effects while dispose()/cleanup() calls can mutate these collections.

Fix: Convert lazy iterables to List snapshots before iteration.

Fixes rodydavis#431
Copy link
Copy Markdown
Owner

@rodydavis rodydavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ✅

@rodydavis rodydavis merged commit 770df30 into rodydavis:main Dec 19, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConcurrentModificationError in SignalsMixin during effect iteration

2 participants