fix: iOS recorder callback teardown race#1057
Open
radko93 wants to merge 1 commit intosoftware-mansion:mainfrom
Open
fix: iOS recorder callback teardown race#1057radko93 wants to merge 1 commit intosoftware-mansion:mainfrom
radko93 wants to merge 1 commit intosoftware-mansion:mainfrom
Conversation
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.
Closes #
None
Introduced changes
IOSRecorderCallback::cleanup()and~IOSRecorderCallback()currently nil the ObjC instance vars (converter_,bufferFormat_,converterInputBuffer_, ...) before destroying theTaskOffloader. The worker thread intaskOffloaderFunctionreads those same ObjC fields without synchronization, so on teardown it can dereference fields that are mid-release.This PR reorders teardown so the worker is joined before any ObjC field is released:
isInitialized_.store(false)— fence further audio-thread enqueues.offloader_.reset()— signals shutdown via the dummy item, joins the worker.In the destructor specifically, the existing implementation relied on the
unique_ptr<TaskOffloader>member destructor (which runs after the body), so the body's nil assignments raced the worker. Explicitly resetting at the top of the body closes that window.cleanup()still flushes any frames the worker had pushed intocircularBuffer_before being joined (emitAudioData(true)). Frames sitting undrained in the SPSC channel at cleanup time are dropped — this matches the prior behaviour, the previous code was racing the same drop incorrectly.The race is not deterministically reproducible (it depends on worker scheduling) so this PR does not add a unit test. We applied this change downstream and confirmed the corresponding production iOS
SIGSEGVsignature drops to zero. Representative symbolicated stack from production (seen across iOS 17/18/26, multiple iPhone models):The crashing frame at
IOSRecorderCallback.mm:166isconverterInputBuffer_.mutableAudioBufferList->mBuffers[i].mData— the worker dereferencing anAVAudioPCMBuffer*instance variable that the JS thread is concurrently nil-ing insidecleanup(). The JS thread is in_pthread_joinwaiting for that same worker, which won't return because it just SIGSEGV'd. With the reorder in this PR,offloader_.reset()(which performs the join) completes before any ObjC field is released, so the worker is provably done before fields are nil.A separate audio-buffer-lifetime issue in the same component is being tracked at #1053 (with proposed fix in #1054); intentionally out of scope for this PR.
Checklist