Commit 501145c
authored
fix: fixed dict and array enumeration for clone method (#26)
### The Problem
The clone method on iOS has two mutation-during-enumeration bugs — a
classic Objective-C pitfall that causes crashes:
1. Dictionary mutation during enumeration — The code iterates over
self.localStreams directly (for ... in self.localStreams) while the loop
body can modify the
dictionary (e.g., adding tracks to a stream can trigger side effects).
In Objective-C, mutating a collection while enumerating it throws an
NSInvalidArgumentException.
2. Array mutation during enumeration — Similarly, stream.audioTracks and
stream.videoTracks are iterated directly, but addAudioTrack: /
addVideoTrack: calls inside the
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.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Bug Fixes**
* Improved reliability of media stream handling to prevent potential
issues during track operations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent d46511f commit 501145c
1 file changed
+6
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
522 | | - | |
| 522 | + | |
523 | 523 | | |
524 | | - | |
| 524 | + | |
| 525 | + | |
525 | 526 | | |
526 | 527 | | |
527 | 528 | | |
| |||
537 | 538 | | |
538 | 539 | | |
539 | 540 | | |
540 | | - | |
| 541 | + | |
541 | 542 | | |
542 | | - | |
| 543 | + | |
| 544 | + | |
543 | 545 | | |
544 | 546 | | |
545 | 547 | | |
| |||
0 commit comments