Skip to content

Fix race condition that was causing a crash in time observer #628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025

Conversation

jbaudanza
Copy link
Contributor

I was only able to reproduce this crash once, but I took a screenshot:

Screenshot 2024-09-15 at 5 01 46 PM

The root cause is that audioPlayer is set to nil on the react-native thread, not the main thread. So, even though we change for nil on line 338, there's no guarantee it's still not nil on line 340.

This PR will wrap the audioPlayer = nil mutation in DispatchQueue.main.async.

I also changed the time observer to keep a weak reference to self, because the apple docs state this is important to prevent a retain cycle: https://developer.apple.com/documentation/avfoundation/avplayer/1385829-addperiodictimeobserverforinterv?language=swift

@hyochan hyochan requested a review from Copilot June 6, 2025 17:58
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a race condition by ensuring audioPlayer is set to nil on the main thread and updates the periodic time observer to use a weak reference to self to avoid retain cycles.

  • Wrapped audioPlayer = nil (and related pause/removeObserver/resolve) in DispatchQueue.main.async
  • Changed time observer closure to capture self weakly and guard unwrapping to prevent retain cycles
Comments suppressed due to low confidence (2)

ios/RNAudioRecorderPlayer.swift:415

  • [nitpick] Unnecessary semicolon in Swift. Removing the semicolon keeps the style consistent with the rest of the file.
self.audioPlayer = nil;

ios/RNAudioRecorderPlayer.swift:338

  • [nitpick] You might also capture audioPlayerItem in the guard (let item = self.audioPlayerItem) to avoid multiple self accesses and guard against it being nil during the time observer callback.
guard let self = self, let audioPlayer = self.audioPlayer else { return }

Copy link
Owner

@hyochan hyochan left a comment

Choose a reason for hiding this comment

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

LGTM!

@hyochan hyochan merged commit 1d99790 into hyochan:main Jun 13, 2025
2 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.

2 participants