Re: Injecting an exception into another thread

Carl Friedrich Bolz-Tereick <[email protected]> Fri, 27 May 2022 12:54:20 +0200
Newsgroups gmane.comp.python.pypy
Message-ID <[email protected]>
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
>

_______________________________________________
pypy-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pypy-dev.python.org/
Member address: [email protected]