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

document behavior of calling atexit.register() while atexit._run_exitfuncs is running #67056

Closed
smontanaro opened this issue Nov 13, 2014 · 6 comments · Fixed by #104473
Closed
Labels
docs Documentation in the Doc dir

Comments

@smontanaro
Copy link
Contributor

smontanaro commented Nov 13, 2014

BPO 22867
Nosy @smontanaro, @vadmium

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 = None
created_at = <Date 2014-11-13.20:52:37.164>
labels = ['docs']
title = 'document behavior of calling atexit.register() while atexit._run_exitfuncs is running'
updated_at = <Date 2015-07-21.07:43:20.900>
user = 'https://github.com/smontanaro'

bugs.python.org fields:

activity = <Date 2015-07-21.07:43:20.900>
actor = 'ethan.furman'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2014-11-13.20:52:37.164>
creator = 'skip.montanaro'
dependencies = []
files = []
hgrepos = []
issue_num = 22867
keywords = []
message_count = 2.0
messages = ['231135', '231354']
nosy_count = 3.0
nosy_names = ['skip.montanaro', 'docs@python', 'martin.panter']
pr_nums = []
priority = 'low'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue22867'
versions = []

Linked PRs

@smontanaro
Copy link
Contributor Author

A discussion on comp.lang.python about prettying up the "if __name__ == 'main__'" idiom led to a suggestion that a decorator could simple register the main function using atexit.register. That looks like it will work, but leaves open the possibility that while main() is running via atexit._run_exitfuncs, other exit functions might be registered.

As currently defined (at least in the Python version with 2.7), I think everything will work fine. Still, the behavior of adding new exit functions during exit is not defined. Would be kind of nice if this behavior was blessed, and then mentioned in the documentation.

@smontanaro smontanaro added the docs Documentation in the Doc dir label Nov 13, 2014
@ethanfurman
Copy link
Member

ethanfurman commented Nov 19, 2014

From a post by Ian Kelly (https://mail.python.org/pipermail/python-list/2014-November/681073.html)
--------------------------------------------------------------
In fact it seems the behavior does differ between Python 2.7 and Python 3.4:

$ cat testatexit.py
import atexit

@atexit.register
def main():
  atexit.register(goodbye)

@atexit.register
def goodbye():
  print("Goodbye")
$ python2 testatexit.py
Goodbye
Goodbye
$ python3 testatexit.py
Goodbye

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel
Copy link
Member

The atexit functions are invoked in LIFO order, so I think it's not a good idea to add more after the first one started running. We don't currently forbid, it and I wouldn't argue in favour of doing so, but we shouldn't encourage it.

@smontanaro
Copy link
Contributor Author

This has been idle for "awhile." Should we just put our heads together and decide how best to document this behavior?

@iritkatriel
Copy link
Member

I'm not sure what the behaviour is. Or rather, what behaviour we want to commit to.

@smontanaro
Copy link
Contributor Author

I'm not sure that committing to any particular behavior is the right thing to do without some more discussion. Maybe just warn that calling atexit.register after _PyAtExit_Call has been invoked is undefined. There's no mention of that corner case in the 3.11 or 3.12 documentation as it stands.

iritkatriel added a commit to iritkatriel/cpython that referenced this issue May 14, 2023
iritkatriel added a commit to iritkatriel/cpython that referenced this issue May 14, 2023
…exit func from within an atexit func is undefined"

This reverts commit d3127cd.
iritkatriel added a commit that referenced this issue May 15, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 15, 2023
…c from within an atexit func is undefined (pythonGH-104473)

(cherry picked from commit b15a1a6)

Co-authored-by: Irit Katriel <[email protected]>
iritkatriel pushed a commit that referenced this issue May 15, 2023
carljm added a commit to carljm/cpython that referenced this issue May 15, 2023
* main: (29 commits)
  pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418)
  pythongh-101819: Isolate `_io` (python#101948)
  Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501)
  pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495)
  pythongh-104050: Run mypy on `clinic.py` in CI (python#104421)
  pythongh-104490: Consistently define phony make targets (python#104491)
  pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473)
  pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488)
  pythongh-101282: move BOLT config after PGO (pythongh-104493)
  pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470)
  pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457)
  pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474)
  pythongh-104337: Clarify random.gammavariate doc entry  (python#104410)
  Minor improvements to typing docs (python#104465)
  pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460)
  pythonGH-71383: IDLE - Document testing subsets of modules (python#104463)
  pythongh-104454: Fix refleak in AttributeError_reduce (python#104455)
  pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446)
  pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424)
  Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430)
  ...
carljm added a commit to carljm/cpython that referenced this issue May 15, 2023
* main: (204 commits)
  pythongh-101819: Fix _io clinic input for unused base class method stubs (python#104418)
  pythongh-101819: Isolate `_io` (python#101948)
  Bump mypy from 1.2.0 to 1.3.0 in /Tools/clinic (python#104501)
  pythongh-104494: Update certain Tkinter pack/place tests for Tk 8.7 errors (python#104495)
  pythongh-104050: Run mypy on `clinic.py` in CI (python#104421)
  pythongh-104490: Consistently define phony make targets (python#104491)
  pythongh-67056: document that registering/unregistering an atexit func from within an atexit func is undefined (python#104473)
  pythongh-104487: PYTHON_FOR_REGEN must be minimum Python 3.10 (python#104488)
  pythongh-101282: move BOLT config after PGO (pythongh-104493)
  pythongh-104469 Convert _testcapi/float.c to use AC (pythongh-104470)
  pythongh-104456: Fix ref leak in _ctypes.COMError (python#104457)
  pythongh-98539: Make _SSLTransportProtocol.abort() safe to call when closed (python#104474)
  pythongh-104337: Clarify random.gammavariate doc entry  (python#104410)
  Minor improvements to typing docs (python#104465)
  pythongh-87092: avoid gcc warning on uninitialized struct field in assemble.c (python#104460)
  pythonGH-71383: IDLE - Document testing subsets of modules (python#104463)
  pythongh-104454: Fix refleak in AttributeError_reduce (python#104455)
  pythongh-75710: IDLE - add docstrings and comments to editor module (python#104446)
  pythongh-91896: Revert some very noisy DeprecationWarnings for `ByteString` (python#104424)
  Add a mention of PYTHONBREAKPOINT to breakpoint() docs (python#104430)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
3 participants