This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2020-04-15 02:16 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 19562 closed corona10, 2020-04-17 01:14
Messages (6)
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".
msg383878 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-12-27 23:43
Fixed by bpo-42639.
History
Date User Action Args
2022-04-11 14:59:29adminsetgithub: 84468
2020-12-27 23:43:32vstinnersetstatus: open -> closed
superseder: Make atexit state per interpreter
messages: + msg383878

resolution: duplicate
stage: patch review -> resolved
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