Repository navigation
AVSpeech: reuse one synthesizer in speak_to_memory to fix a teardown crash under rapid synthesis - #80
Conversation
Allocating a new AVSpeechSynthesizer per call and destroying it from the calling thread races TextToSpeech's teardown (on its AXSpeech thread) against callback blocks still queued on the main dispatch queue; under sustained rapid synthesis a late block dereferences freed state and crashes (EXC_BAD_ACCESS, KERN_INVALID_ADDRESS). Keep one long-lived synthesizer for memory synthesis, mirroring the live speak path: create it lazily on the main thread, stop it before reuse if a prior utterance is still active, and release it in the destructor.
|
@aaronr7734 Can you explain why in your diff you rewrote the entire Additionally, can you provide the code that triggers this reliably? Please ensure that it is in the form of a test so that we can start creating a regression suite. |
|
Thanks for the review. Both cleanups are pushed. On the diff churn: that was a clang-format artifact, not a rewrite. Renaming the receiver from On a reliable trigger: I'm not sure that a deterministic reproduction is possible here, because the race is inside Apple's TextToSpeech framework. As far as I can tell, the crash depends on whether a queued callback block on the main queue runs before or after the framework's AXSpeech thread finishes tearing down the synthesizer. What I can offer is the full crash report, attached. One thing I did notice when reviewing the crash report again is the crashed frame's program counter is unattributed, because the process ran x86_64 under Rosetta 2 and the PC lands in translated code that belongs to no binary image. The TextToSpeech connection is inferred from the rest of the report the crashing thread was executing a queued main-queue block, while the AXSpeech thread was simultaneously mid-dealloc of TextToSpeech objects (including a Combine Anyway, the crash dump can be found here. |
|
@aaronr7734 That is interesting. Can you test under ordinary execution (non-Rosetta)? I'm curious if this reoccurs there. I would like to get some kind of reliable reproduction if at all possible, but race conditions are always annoying to (reliably) trigger. |
Summary
AVSpeechBackend::speak_to_memoryallocates a newAVSpeechSynthesizerforevery call and lets the calling thread drop the last reference when the call
returns. Destroying a synthesizer while Apple's TextToSpeech framework still
has callback blocks for it queued on the main dispatch queue is racy: the
teardown runs on the framework's AXSpeech thread, and a late callback block
then dereferences freed state and crashes the process (EXC_BAD_ACCESS,
KERN_INVALID_ADDRESS).
This is hard to hit with occasional synthesis, but a caller that synthesizes
many short utterances in quick succession — dozens of
speak_to_memorycallsper minute, sustained — loses the race within minutes.
This PR makes the backend keep one long-lived synthesizer for memory
synthesis, mirroring what the class already does for the live
speakpath(the
synthesizermember). No API or behavior change otherwise.Crash evidence
Observed on macOS 26 in an x86_64 process running under Rosetta 2. The crash
report shows:
EXC_BAD_ACCESS / KERN_INVALID_ADDRESS at 0x10inside a
dispatch_call_block_and_releaseblock whose program counter is inTextToSpeech.framework.AXSpeechthread is mid-teardown of TextToSpeechobjects: a chain of
_objc_rootDealloc/object_cxxDestructFromClassframes through TextToSpeech, including a Combine
CurrentValueSubjectdeinit.
speak_to_memorypath; every call created and destroyed one synthesizer.I can attach the full .ips crash report if useful.
Fix
memory_synthesizermember, created lazily on the main thread on thefirst
speak_to_memorycall and reused for the life of the backend, so nosynthesizer teardown ever runs while the framework may still hold callbacks.
timed out), stop it with
AVSpeechBoundaryImmediatebefore writing the nextutterance.
synthesizermember.One file changed,
source/backends/avspeech.cpp; formatted with the repo's.clang-format. Most of the diff's line count is the
writeUtterance:callbody re-indenting under the renamed receiver.
Why this is safe
speak_to_memoryalready serializes per backend instance in practice (thecaller waits on the accumulator semaphore before issuing the next call), so
the reused synthesizer never sees overlapping write requests, and the
stop-before-reuse guard covers the timeout path.
volume are set on each
AVSpeechUtterance, not on the synthesizer.for dropping a full registration + teardown cycle per utterance.
Testing
compiles with no new warnings.
exact path with rapid short-utterance synthesis. Before the change, the
crash was reproducible under sustained use with the signature above; with
the patch applied, the same workload runs without crashes or audio
regressions.