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

[subinterpreters] atexit module should not be loaded more than once per interpreter #84468

Closed
vstinner opened this issue Apr 15, 2020 · 6 comments
Labels
3.9 only security fixes topic-subinterpreters

Comments

@vstinner
Copy link
Member

BPO 40288
Nosy @vstinner, @ericsnowcurrently, @corona10, @shihai1991
PRs
  • bpo-40288: atexit supports more than once loading per interpreter  #19562
  • Superseder
  • bpo-42639: Make atexit state per interpreter
  • 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-27.23:43:32.781>
    created_at = <Date 2020-04-15.02:16:49.684>
    labels = ['expert-subinterpreters', '3.9']
    title = '[subinterpreters] atexit module should not be loaded more than once per interpreter'
    updated_at = <Date 2020-12-27.23:43:32.779>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-12-27.23:43:32.779>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-12-27.23:43:32.781>
    closer = 'vstinner'
    components = ['Subinterpreters']
    creation = <Date 2020-04-15.02:16:49.684>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40288
    keywords = ['patch']
    message_count = 6.0
    messages = ['366478', '366517', '368666', '376670', '376673', '383878']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'eric.snow', 'corona10', 'shihai1991']
    pr_nums = ['19562']
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '42639'
    type = None
    url = 'https://bugs.python.org/issue40288'
    versions = ['Python 3.9']

    @vstinner
    Copy link
    Member Author

    Since Python 3.7, it's possible to load the atexit module more than once:

    commit 776407f
    Author: Marcel Plch <gmarcel.plch@gmail.com>
    Date: Wed Dec 20 11:17:58 2017 +0100

    bpo-31901: atexit callbacks should be run at subinterpreter shutdown (bpo-4611)
    
    Change atexit behavior and PEP-489 multiphase init support.
    

    Each new import executes the module which overrides PyInterpreterState.pyexitfunc with _Py_PyAtExit().

    Example:
    ---

    import sys
    
    atexit1 = sys.modules.pop('atexit', None)
    if atexit1 is None:
        import atexit as atexit1
        del sys.modules['atexit']
    
    import atexit as atexit2
    
    atexit1.register(print, "atexit1 callback")
    atexit2.register(print, "atexit2 callback")

    Output:
    ---
    atexit2 callback
    ---

    Either PyInterpreterState should support a list of exit functions, or atexit should raise an exception if it's loaded more than once.

    call_ll_exitfuncs() calls a list of functions: _PyRuntimeState.exitfuncs. But these functions are called at the end of Py_Finalize(), whereas atexit functions are called after calling threading._shutdown() in Py_Finalize() and Py_EndInterpreter().

    @vstinner vstinner added stdlib Python modules in the Lib dir 3.9 only security fixes labels Apr 15, 2020
    @corona10
    Copy link
    Member

    I will take a look :)

    @vstinner
    Copy link
    Member Author

    I created bpo-40600: "Add an option to disallow creating more than one instance of a module".

    @vstinner vstinner added topic-subinterpreters and removed stdlib Python modules in the Lib dir labels May 15, 2020
    @vstinner vstinner changed the title atexit module should not be loaded more than once per interpreter [subinterpreters] atexit module should not be loaded more than once per interpreter May 15, 2020
    @vstinner vstinner added topic-subinterpreters and removed stdlib Python modules in the Lib dir labels May 15, 2020
    @vstinner vstinner changed the title atexit module should not be loaded more than once per interpreter [subinterpreters] atexit module should not be loaded more than once per interpreter May 15, 2020
    @corona10
    Copy link
    Member

    @vstineer
    I'd like to update PR 19562 not to create more than one instance.
    if the PR is updated, would you like to review this PR?

    @vstinner
    Copy link
    Member Author

    I would prefer a more generic solution, if possible:

    bpo-40600: "Add an option to disallow creating more than one instance of a module".

    @vstinner
    Copy link
    Member Author

    Fixed by bpo-42639.

    @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.9 only security fixes topic-subinterpreters
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants