Fix Collection was modified crash in ManualMotionDispatcher when scheduling during Update#291
Open
Sahasrara wants to merge 1 commit into
Open
Conversation
…ng during Update ManualMotionDispatcher.Update and Reset enumerated the runners dictionary directly. A motion callback invoked during iteration (Bind/OnComplete/ OnCancel/loop callbacks) can schedule the first motion of a new type, which lazily adds a runner via GetOrCreateRunner and mutates the dictionary mid-enumeration, throwing "InvalidOperationException: Collection was modified; enumeration operation may not execute." Iterate a reused buffer snapshot of runners.Values instead of the live dictionary. The buffer is reused across calls and filled via AddRange's ICollection.CopyTo fast path, so this remains allocation-free after warmup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ManualMotionDispatcher.UpdateandResetenumerate therunnersdictionary directly:The only place
runnersis mutated isrunners.Add(...)inGetOrCreateRunner, which runs lazily the first time a given(TValue, TOptions, TAdapter)motion type is scheduled on the dispatcher.During
runner.Update(...), motion callbacks fire (per-frameBindactions,WithOnComplete,WithOnCancel, loop callbacks). If such a callback schedules the first motion of a not-yet-seen type through this dispatcher,GetOrCreateRunneradds a new entry torunnerswhile theforeachis iterating it, throwing:It's intermittent because it only triggers the first time each new motion type is introduced from inside a callback during
Update.Reset()has the same latent issue (a cancel callback can schedule a new-type motion).Fix
Iterate a snapshot of
runners.Valuesinstead of the live dictionary, so a runner added by a callback mid-iteration no longer invalidates the enumeration.The snapshot uses a reused
List<IUpdateRunner>buffer, filled viaAddRange(runners.Values)(which takesList'sICollection.CopyTofast path;Dictionarycaches itsValueCollection). So it stays allocation-free after warmup — no per-frame allocation is introduced. A runner created duringUpdatesimply ticks on the next frame.Notes
UpdateandReset.