Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make atexit state per interpreter #86805

Closed
vstinner opened this issue Dec 14, 2020 · 7 comments
Closed

Make atexit state per interpreter #86805

vstinner opened this issue Dec 14, 2020 · 7 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 42639
Nosy @vstinner, @corona10, @shihai1991
PRs
  • bpo-42639: Move atexit state to PyInterpreterState #23763
  • bpo-42639: Cleanup atexitmodule.c #23770
  • bpo-42639: atexit now logs callbacks exceptions #23771
  • bpo-40600: atexit only loads single time per interpreter #23699
  • bpo-40600: Disallow loading extension modules load more than once in per interpreter. #23683
  • bpo-42639: Add script_helper.run_test_script() #23777
  • bpo-42639: atexit._run_exitfuncs() uses sys.unraisablehook #23779
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-12-15.16:17:11.642>
    created_at = <Date 2020-12-14.12:00:33.149>
    labels = ['library', '3.10']
    title = 'Make atexit state per interpreter'
    updated_at = <Date 2020-12-15.16:17:11.637>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-12-15.16:17:11.637>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-15.16:17:11.642>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2020-12-14.12:00:33.149>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42639
    keywords = ['patch']
    message_count = 7.0
    messages = ['382982', '383005', '383008', '383055', '383064', '383071', '383073']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'corona10', 'shihai1991']
    pr_nums = ['23763', '23770', '23771', '23699', '23683', '23777', '23779']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42639'
    versions = ['Python 3.10']

    @vstinner
    Copy link
    Member Author

    In Python 2.7, atexit was implemented in Python and registered itself using sys.exitfunc public attribute:
    https://docs.python.org/2.7/library/sys.html#sys.exitfunc
    https://docs.python.org/2.7/library/atexit.html#module-atexit

    In Python 3.0, the atexit module was rewritten in C. A new private _Py_PyAtExit() function was added to set a new private global "pyexitfunc" variable: variable used by call_py_exitfuncs() called by Py_Finalize().

    In Python 3.7, the global "pyexitfunc" variable was moved int _PyRuntimeState (commit 2ebc5ce), and then into PyInterpreterState (commit 776407f).

    In Python 3.7, the atexit module was upgrade to the multiphase initialization API (PEP-489): PyInit_atexit() uses PyModuleDef_Init().

    Since Python 2.7, the atexit module has a limitation: if a second instance is created, the new instance overrides the old one, and old registered callbacks are newer called.

    One option is to disallow creating a second instance: see bpo-40600 and PR 23699 for that.

    Another option is to move the atexit state (callbacks) into PyInterpreterState. Two atexit module instances would modify the same list of callbacks. In this issue, I propose to investigate this option.

    @vstinner vstinner added 3.10 only security fixes stdlib Python modules in the Lib dir labels Dec 14, 2020
    @vstinner
    Copy link
    Member Author

    New changeset 83d5204 by Victor Stinner in branch 'master':
    bpo-42639: Cleanup atexitmodule.c (GH-23770)
    83d5204

    @vstinner
    Copy link
    Member Author

    New changeset 357704c by Victor Stinner in branch 'master':
    bpo-42639: atexit now logs callbacks exceptions (GH-23771)
    357704c

    @vstinner
    Copy link
    Member Author

    New changeset b8fa135 by Victor Stinner in branch 'master':
    bpo-42639: Move atexit state to PyInterpreterState (GH-23763)
    b8fa135

    @vstinner
    Copy link
    Member Author

    New changeset f7049b5 by Victor Stinner in branch 'master':
    bpo-42639: Add script_helper.run_test_script() (GH-23777)
    f7049b5

    @vstinner
    Copy link
    Member Author

    New changeset 3ca2b8f by Victor Stinner in branch 'master':
    bpo-42639: atexit._run_exitfuncs() uses sys.unraisablehook (GH-23779)
    3ca2b8f

    @vstinner
    Copy link
    Member Author

    Ok, the initial issue is now fixed. It's possible to have more than one atexit module instance, registered callbacks in one or the other module are all called as expected. I also wrote an unit test for that.

    I also took the opportunity of this issue to modernize the C code base, fix a bug in test_atexit (no longer clear atexit callbacks, run tests in subprocesses), and enhance atexit._run_exitfuncs(): log all callback exceptions using sys.unraisablehook so it's possible to catch them using the hook.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant