Skip to content

Switch webrtc from multiprocessing to threading#2154

Draft
yeldarby wants to merge 1 commit intomainfrom
fix-local-webrtc
Draft

Switch webrtc from multiprocessing to threading#2154
yeldarby wants to merge 1 commit intomainfrom
fix-local-webrtc

Conversation

@yeldarby
Copy link
Copy Markdown
Contributor

@yeldarby yeldarby commented Mar 25, 2026

What does this PR do?

The local WebRTC worker (/initialise_webrtc_worker) was crashing with SIGSEGV on macOS because multiprocessing.get_context("spawn") forces a child process to re-import and re-initialize all C extensions (PyTorch, av, aiortc), which segfaults in native code.

Fix: Replace the multiprocessing subprocess with a threading.Thread running its own asyncio.new_event_loop() for the local (non-Modal) code path. The SDP answer is communicated back via asyncio.Future + call_soon_threadsafe. This avoids subprocess spawn entirely, eliminating the segfault. The Modal (production) path is unchanged.

Also added defensive error handling in cpu.py so that if the multiprocessing path is ever used, unhandled exceptions are logged and sent back through the pipe instead of silently killing the child process.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • I have tested this change locally

Test details:

  • Verified /initialise_webrtc_worker no longer crashes with SIGSEGV on macOS
  • Confirmed WebRTC peer connection establishes successfully via the threaded worker

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings or errors

@grzegorz-roboflow
Copy link
Copy Markdown
Collaborator

I'd vote for adding 3rd option to run workers - we can have 3 options coexisting and turned on based on user requirements

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