Injecting an exception into another thread

Hello, I'm trying to replicate the behavior you can get in CPython with the following code: ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(KeyboardInterrupt)) This schedules an exception to be raised on the target thread. I saw one reference indicating that the CPython interpreter implements this by polling for exceptions to raise every hundred opcodes or so, but I didn't look into it and don't know for sure that's correct. It does seem correct, though, based just on my intuition of how a runtime would need to handle asynchronous signals. Is there any equivalent functionality in pypy? If not, is there anything fundamentally preventing pypy from gaining something like this? If not, is there anything I can do to help it be built? Thanks, - Audrey -- Audrey Dutcher she/her/hers https://rhelmot.io/

Hi Audrey, Welcome! Thanks for your question! So PyPy has the internal functionality to asynchronous signals at safepoints. We use it for example to call __del__ methods of collected objects at safe positions, for signals, and also to implement _thread.interrupt_main. The mechanism in PyPy is a bit different than counting opcodes, but that difference is not super important for the discussion. We don't have an API exposed at the moment that could would use this mechanism to deliver exceptions to threads though. It could be built, and would probably not be super hard. Could you explain a little bit what your use case is? Cheers, Carl Friedrich On 27.05.22 09:51, Audrey Dutcher wrote:
Hello,
I'm trying to replicate the behavior you can get in CPython with the following code:
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(KeyboardInterrupt))
This schedules an exception to be raised on the target thread. I saw one reference indicating that the CPython interpreter implements this by polling for exceptions to raise every hundred opcodes or so, but I didn't look into it and don't know for sure that's correct. It does seem correct, though, based just on my intuition of how a runtime would need to handle asynchronous signals.
Is there any equivalent functionality in pypy? If not, is there anything fundamentally preventing pypy from gaining something like this? If not, is there anything I can do to help it be built?
Thanks, - Audrey
participants (2)
-
Audrey Dutcher
-
Carl Friedrich Bolz-Tereick