fix: fixed dict and array enumeration for clone method#26
Conversation
📝 WalkthroughWalkthroughThis change enhances the safety of media stream cloning in the WebRTC module by adopting defensive iteration patterns. Dictionary and collection iteration are now performed over copies or explicit keys, with added nil-checks for streams, preventing potential crashes from mutations during enumeration while preserving existing functionality. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The Problem
The clone method on iOS has two mutation-during-enumeration bugs — a classic Objective-C pitfall that causes crashes:
dictionary (e.g., adding tracks to a stream can trigger side effects). In Objective-C, mutating a collection while enumerating it throws an NSInvalidArgumentException.
loop can mutate the underlying array.
The Fix (two changes, applied to both audio and video track cloning)
Before: for (... in self.localStreams)
After: for (... in [self.localStreams allKeys])
Why: Iterates over a snapshot of the dictionary keys, so mutations to the dictionary during the loop are safe.
────────────────────────────────────────
Before: for (... in stream.audioTracks) / stream.videoTracks
After: for (... in [stream.audioTracks copy]) / [stream.videoTracks copy]
Why: Iterates over a copy of the array, so adding tracks to the stream mid-loop won't crash.
────────────────────────────────────────
Before: (no nil check)
After: if (stream == nil) continue;
Why: Adds a defensive nil guard in case a stream was removed between getting the keys and looking it up.
Impact
This is a crash fix for the iOS track cloning path. Without it, cloning a media stream track could intermittently crash the app with a "collection was mutated while
being enumerated" exception — a race condition that's hard to reproduce but common under load.
Summary by CodeRabbit