Skip to content

[BUG]: Python atexit broken with pybind11 version >= 2.9.2 #4459

Description

@chrreisinger

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.10.2

Problem description

We want to upgrade from pybind11 release 2.9.0 to 2.10.2, but we encountered an issue in the Python atexit (https://docs.python.org/3/library/atexit.html) functions, that our custom types are not registered anymore. With delta debugging we found that this PR caused the issue: #3744

In my opinion the

    detail::get_local_internals().registered_types_cpp.clear();    
    detail::get_local_internals().registered_exception_translators.clear();

calls should be after the Py_Finalize function call.

When I change the orders of those function calls, the problem is gone.

Reproducible example code

No response

Is this a regression? Put the last known working version here if it is.

2.9.1

Activity

  1. Skylion007 commented on Jan 18, 2023

    @Skylion007
    Collaborator

    FYI @rwgk

  2. rwgk commented on Jan 19, 2023

    @rwgk
    Collaborator

    @chrreisinger could you help by

    • creating a PR that moves the .clear() calls after the Py_Finalize() call,
    • and ideally adds a unit test for your situation (in tests/test_embed/test_interpreter.cpp)?

    The change makes sense to me. My understanding is: the Py_Finalize() invalidates all PyObject* pointers in the local_internals, but looking at the struct local_internals code, it seems to me the .clear() calls don't ever use those pointers. There are a bunch of non-trivial destructors that will run, but none of them will call into the Python C API.

  3. chrreisinger commented on Jan 19, 2023

    @chrreisinger
    Author

    I will give it a try on the weekend.

  4. rwgk commented on Jan 19, 2023

    @rwgk
    Collaborator

    I will give it a try on the weekend.

    Awesome.

    If you create a PR with just the lines moved, that'll be a great start. It will be good to confirm that valgrind does not find any issues. We can work on the test together from there.

  5. Skylion007 commented on Jan 29, 2023

    @Skylion007
    Collaborator

    @chrreisinger Any updates on this?

  6. added a commit that references this issue on Feb 2, 2023
    a29a591
  7. added and removed
    triageNew bug, unverified
    on Feb 4, 2023
  8. added a commit that references this issue on Feb 6, 2023
    b2c1978
  9. added a commit that references this issue on Feb 8, 2023
    4efdec8
  10. Skylion007 commented on Feb 8, 2023

    @Skylion007
    Collaborator

    Oh boy, this broke some more things.

  11. added a commit that references this issue on Feb 8, 2023
    7d63557
  12. henryiii commented on Jul 25, 2026

    @henryiii
    Collaborator

    🤖 AI text below 🤖

    The finalization order was rewritten by #5564. embed.h now calls Py_Finalize() before it destroys the internals, which is what this issue asked for. Python atexit handlers therefore run while the bindings are still alive.

    If you can still reproduce this with current master (v3), please comment and we can reopen.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions