Skip to content

PyEval_AcquireThread can crash process if called from another thread at interpreter shutdown #117762

Open
@ezyang

Description

@ezyang

🐛 Describe the bug

Meta xref: https://fb.workplace.com/groups/1405155842844877/posts/7910584775635252/?comment_id=7912862145407515&reply_comment_id=7917182454975484&notif_id=1705598818428473&notif_t=work_group_comment_mention

Quote:

Attempted to use _Py_IsFinalizing() to guard against finalize, but it always returns false (even though the coredump indicates the interpreter thread is within Py_FinalizeEx() and should have set the finalize attribute on the runtime...but the whole thing is racy): D52866385
Also tried just catching the exception with try {...} catch (...) {} but the program still aborts with FATAL: exception not rethrown.

The exact line that fails is we create a new PyThreadState and then try to swap it and lock the GIL w/ PyEval_AcquireThread in the pybind11 code.

The docs for PyEval_AcquireThread suggest that the calling thread will be terminate if the interpreter is finalizing, which matches the behavior we get. https://docs.python.org/3/c-api/init.html...

The problem is I don't know how to avoid that. I tried _Py_IsFinalizing(), it returns false everywhere. I attempted to use Py_AtExit to schedule a callback where I could implement my own synchronization mechanism, but it only gets scheduled half the time.

Versions

main

cc @albanD

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: python frontendFor issues relating to PyTorch's Python frontendtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions