Skip to content

fix: add mutex to VoiceActivityDetection to prevent race between generate() and unload()#1056

Merged
msluszniak merged 2 commits intosoftware-mansion:mainfrom
radko93:fix/vad-thread-safety
Apr 8, 2026
Merged

fix: add mutex to VoiceActivityDetection to prevent race between generate() and unload()#1056
msluszniak merged 2 commits intosoftware-mansion:mainfrom
radko93:fix/vad-thread-safety

Conversation

@radko93
Copy link
Copy Markdown
Contributor

@radko93 radko93 commented Apr 6, 2026

Description

Add thread-safety to VoiceActivityDetection using the same mutex pattern already established by VisionModel.

Without this, BaseModel::unload() can destroy module_ on the JS thread while generate() is still calling forward() on a worker thread, causing SIGILL / SIGSEGV crashes.

Introduces a breaking change?

  • No

Type of change

  • Bug fix (change which fixes an issue)

Tested on

  • Android

Testing instructions

Use useVAD({ model: FSMN_VAD }) with continuous audio input and trigger rapid mount/unmount cycles. Confirm no crashes or deadlocks.

Related issues

Fixes #1055

…rate() and unload()

VoiceActivityDetection inherits from BaseModel but lacks the thread-safety
that VisionModel already provides. When generate() runs on a worker thread
(via ModelHostObject::promiseHostFunction) and unload() is called from the
JS thread, BaseModel::unload() destroys module_ mid-inference, causing
SIGILL/SIGSEGV crashes.

This applies the same pattern used by VisionModel:
- Add inference_mutex_ member
- Lock in generate() to protect forward() calls
- Override unload() to acquire the lock before BaseModel::unload()

Fixes software-mansion#1055
@msluszniak msluszniak added the bug fix PRs that are fixing bugs label Apr 6, 2026
@barhanc
Copy link
Copy Markdown
Contributor

barhanc commented Apr 7, 2026

The problem with race condition occurs also in other models that do not inherit from VisionModel and thus we probably want to fix it on the BaseModel.cpp implementation level. Could you check if fixes introduced in branch @bh/issue1055 solve your problem?

@radko93
Copy link
Copy Markdown
Contributor Author

radko93 commented Apr 7, 2026

@barhanc unfortunately I would need to deploy that to production to really find out. It is possible if it would be safe but it's complicated as you can understand

Copy link
Copy Markdown
Contributor

@barhanc barhanc left a comment

Choose a reason for hiding this comment

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

We will apply fixes on per-model basis mirroring what you did for VAD. Thanks for submitting this fix.

…ice_activity_detection/VoiceActivityDetection.h
@barhanc barhanc requested a review from msluszniak April 7, 2026 17:20
@msluszniak msluszniak merged commit 92981bf into software-mansion:main Apr 8, 2026
4 checks passed
@radko93 radko93 deleted the fix/vad-thread-safety branch April 8, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug fix PRs that are fixing bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android SIGILL crash in VAD unload path

3 participants