Skip to content

Fix macOS main-thread runloop deadlock in wait_for_playback/wait_for_property (Fixes #61) - #317

Closed
klintworthbowthorpe wants to merge 1 commit into
jaseg:mainfrom
klintworthbowthorpe:fix-macos-threading-61
Closed

klintworthbowthorpe wants to merge 1 commit into
jaseg:mainfrom
klintworthbowthorpe:fix-macos-threading-61

Conversation

@klintworthbowthorpe

Copy link
Copy Markdown

Summary of Changes

Fixes #61 ("Stuck in threading on macOS").

Problem

On macOS, libmpv event dispatching and window rendering require an active Cocoa/CoreFoundation runloop on the main thread. When calling wait_for_playback() or wait_for_property() from the main thread, python-mpv previously blocked unconditionally on Future.result(), which invokes threading.Condition.wait(). This puts the Python main thread to sleep in pthread condition wait without pumping the macOS run loop, causing Cocoa UI events to freeze, the dock icon to bounce indefinitely, and playback/property events to deadlock.

Solution

  • Implemented _pump_macos_main_loop() using native ctypes bindings to /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (CFRunLoopRunInMode), requiring zero third-party dependencies (no PyObjC required).
  • Added _wait_for_future(future, timeout=None) in MPV. When running on the macOS main thread, it slices the wait into 20ms intervals and pumps the default run loop while releasing the Python GIL to allow background event handling threads to execute concurrently.
  • Preserved existing zero-overhead blocking behavior on Linux, Windows, and secondary threads.
  • Added regression tests in tests/test_macos_threading.py covering timeout, exceptions, main/secondary thread behavior, and actual playback.

@klintworthbowthorpe

Copy link
Copy Markdown
Author

Closing this PR—this approach was not verified on a physical macOS Cocoa/AppKit display environment. Apologies for the noise.

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.

Stuck in threading on macOS

1 participant