Skip to content

gh-127041: Acquire PEP 788 finalization guard in _PyXI_Enter - #162004

Open
utam-1 wants to merge 2 commits into
python:mainfrom
utam-1:fix-127041-pep788-guard
Open

utam-1 wants to merge 2 commits into
python:mainfrom
utam-1:fix-127041-pep788-guard

Conversation

@utam-1

@utam-1 utam-1 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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 with InterpreterError("interpreter is being destroyed").

  • At the end of _exit_session(): Release the guard after thread-state cleanup and swap-back, so Py_EndInterpreter() waits for any in-flight session to exit before finalizing.

Comment thread Python/crossinterp.c

// 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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@utam-1 utam-1 Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please determine the source of the deadlock. I think it's a bad idea to rely on guard internals here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants