Repository navigation
Conversation
|
|
||
| // Acquire PEP 788 finalization guard before switching or running. | ||
| // If finalization has already started, TryAcquire will fail safely. | ||
| if (_PyInterpreterGuard_TryAcquire(interp, &session->guard) < 0) { |
There was a problem hiding this comment.
We should try to use the public API (PyInterpreterGuard_FromCurrent) and store the result as a pointer. That would also remove the need for the extra has_guard field, because NULL could act as has_guard = 0.
There was a problem hiding this comment.
I tried using the PyInterpreterGuard_FromCurrent() but that resulted in a deadlock in _PyThreadState_Attach, reason could be the behaviour of _enter_session ( Not super sure though).
Instead, since PyInterpreterGuard wraps PyInterpreterState *interp (NULL when zero-initialized), I removed has_guard by checking session->guard.interp != NULL while keeping _PyInterpreterGuard_TryAcquire before _enter_session().
There was a problem hiding this comment.
Please determine the source of the deadlock. I think it's a bad idea to rely on guard internals here.
Based on the feedback I have utilized the PEP 788 finalization guard API (
_PyInterpreterGuard_TryAcquire / _PyInterpreterGuard_Release, added in #128640) inside_PyXI_session, as follows:Before
_enter_session(): Call_PyInterpreterGuard_TryAcquire(). If destruction has already started, fail cleanly withInterpreterError("interpreter is being destroyed").At the end of
_exit_session(): Release the guard after thread-state cleanup and swap-back, soPy_EndInterpreter()waits for any in-flight session to exit before finalizing._interpreters.destroy#127041