-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SIGTRAP when using threading events to wait for completion handler #609
Comments
Completion handlers in general only get called when there's an active Cocoa event loop (the exception being when you can specify a dispatch queue, those often send work to a background thread and call the completion handler on a background thread as well). That's not the case here though. The completion handler gets called in a background thread and there appears to be a race condition between cleaning up on the background thread (which is a daemon thread as far as the CPython runtime is concerned) and the main thread exiting. A quick workaround for this is to add a short sleep after waiting for the completion_event. On my system I'm keeping the issue open because I want to do further research to see if I can avoid the crash (or have to file an issue with the cpython project). |
Thanks @ronaldoussoren - are there any example implementations or recommendations on how to work with completion handlers? I tried some simple implementations with |
I don't have a good solution at this moment, and is something I want to research over the summer anyway because issues like this will crop up more given Apple's switch to Swift and the concurrency model of Swift (which is spelled as "async/await" but uses pervasive threading) I hope to have some time over the summer to finish some work on asyncio integration for PyObjC, including adding async methods for all Objective-C selectors with a completion handler argument. This should help hide most of the complexity in shared code, and should also result in cleaner code in scripts. That should end up with code like this for your problem: import asyncio
from ScreenCaptureKit import SCShareableContent
async def get_shareable_applications():
return await SCShareableContent.getShareableContent()
asyncio.run(get_shareable_applications()) I still have to work out a good design though, and this likely requires using a PyObjC implementation of an asyncio runloop in general (but not necessarily in this use case). |
Thanks, appreciate it. If you come across a good workaround, I'd be interested in trying it. An API for these kinds of situations (e.g. async methods) would be great. |
I'm new to pyobjc so this definitely could be a beginner mistake.
I'm trying to figure out the best way to wait for completion handlers to finish and encapsulate that into a simpler synchronous-like function. I've been testing with
threading.Event
and although it generally works, I've noticed that if the program exits immediately after, I see a SIGTRAP (zsh: trace trap ...
). Is there a proper/better way to do this?Running Python 3.12 on M1 Pro 14.1.2
Thanks!
The text was updated successfully, but these errors were encountered: