Fix macOS threading deadlock in _event_generator (issue #61)#314
Open
copernicusjones wants to merge 1 commit into
Open
Fix macOS threading deadlock in _event_generator (issue #61)#314copernicusjones wants to merge 1 commit into
copernicusjones wants to merge 1 commit into
Conversation
Replace blocking mpv_wait_event(handle, -1) with a pipe-based wakeup mechanism to prevent deadlocks on macOS where Cocoa requires the main thread for its event loop. - Create a self-pipe for wakeup signaling - Register mpv_set_wakeup_callback to write to the pipe when events arrive - Use select.select() on the pipe fd with 100ms timeout instead of blocking forever on mpv_wait_event - Drain events with mpv_wait_event(handle, 0) (non-blocking) after each wakeup or timeout - Clean up pipe and unregister callback in finally block This resolves the deadlock where wait_for_playback() would hang forever on macOS because: 1. Main thread blocks on result.result() waiting for end_file event 2. Event thread blocks on mpv_wait_event(handle, -1) 3. Cocoa needs the main thread to process events, but it's blocked 4. No events are ever delivered → deadlock Closes jaseg#61
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.
Summary
Fixes the deadlock described in issue #61 where
wait_for_playback()hangs forever on macOS.Root Cause
On macOS, mpv's Cocoa backend requires the main thread for its event loop. The previous code used
mpv_wait_event(handle, -1)(infinite timeout) in a background Python thread, which caused a deadlock:result.result()waiting for theend_fileeventmpv_wait_event(handle, -1)Fix
Replace the blocking event loop with a pipe-based wakeup mechanism:
mpv_set_wakeup_callbackto write to the pipe when events arriveselect.select()on the pipe fd with 100ms timeout instead of blocking forevermpv_wait_event(handle, 0)(non-blocking) after each wakeupfinallyblockTesting
python3 -m py_compile mpv.pypassesCloses #61