classification
Title: [subinterpreters] atexit module should not be loaded more than once per interpreter
Type: Stage: patch review
Components: Subinterpreters Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: corona10, eric.snow, shihai1991, vstinner
Priority: normal Keywords: patch

Created on 2020-04-15 02:16 by vstinner, last changed 2020-09-16 04:44 by shihai1991.

Pull Requests
URL Status Linked Edit
PR 19562 open corona10, 2020-04-17 01:14
Messages (5)
msg366478 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-04-15 02:16
Since Python 3.7, it's possible to load the atexit module more than once:

commit 776407fe893fd42972c7e3f71423d9d86741d07c
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 (#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().
msg366517 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2020-04-15 13:56
I will take a look :)
msg368666 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-05-11 22:12
I created bpo-40600: "Add an option to disallow creating more than one instance of a module".
msg376670 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2020-09-10 03:22
@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?
msg376673 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-09-10 06:55
I would prefer a more generic solution, if possible:

> bpo-40600: "Add an option to disallow creating more than one instance of a module".
History
Date User Action Args
2020-09-16 04:44:41shihai1991setnosy: + shihai1991
2020-09-10 06:55:34vstinnersetmessages: + msg376673
2020-09-10 03:22:36corona10setmessages: + msg376670
2020-05-15 00:43:26vstinnersetcomponents: + Subinterpreters, - Library (Lib)
title: atexit module should not be loaded more than once per interpreter -> [subinterpreters] atexit module should not be loaded more than once per interpreter
2020-05-11 22:12:52vstinnersetmessages: + msg368666
2020-04-20 17:28:24corona10setnosy: + eric.snow
2020-04-17 01:14:13corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request18906
2020-04-15 13:56:13corona10setmessages: + msg366517
2020-04-15 02:16:49vstinnercreate