Skip to content

Comments

fix: fixed dict and array enumeration for clone method#26

Merged
santhoshvai merged 1 commit intomasterfrom
fix/clone-unsafe-enumeration
Feb 13, 2026
Merged

fix: fixed dict and array enumeration for clone method#26
santhoshvai merged 1 commit intomasterfrom
fix/clone-unsafe-enumeration

Conversation

@greenfrvr
Copy link

@greenfrvr greenfrvr commented Feb 13, 2026

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.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of media stream handling to prevent potential issues during track operations.

@greenfrvr greenfrvr self-assigned this Feb 13, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 13, 2026

📝 Walkthrough

Walkthrough

This 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

Cohort / File(s) Summary
WebRTC Media Stream Cloning
ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m
Modified mediaStream cloning paths to iterate over dictionary keys and collection copies instead of direct references; added nil-checks before track enumeration to prevent mutation-during-enumeration crashes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hoppy fix for streaming woes,
No more crashes when the media flows,
Copies made before we iterate,
Safe enumeration—how first-rate!
The tracks now clone without a fright, 🎬✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing unsafe dictionary and array enumeration in the clone method by using safe iteration patterns with copies.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into master

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/clone-unsafe-enumeration

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@santhoshvai santhoshvai merged commit 501145c into master Feb 13, 2026
4 checks passed
@santhoshvai santhoshvai deleted the fix/clone-unsafe-enumeration branch February 13, 2026 13:11
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.

2 participants