Skip to content

Reword atexit docs - #156086

Merged
encukou merged 1 commit into
python:mainfrom
encukou:atexit-interpreter
Sep 3, 2026
Merged

Reword atexit docs#156086
encukou merged 1 commit into
python:mainfrom
encukou:atexit-interpreter

Conversation

@encukou

@encukou encukou commented Aug 20, 2026

Copy link
Copy Markdown
Member

For the atexit module:

  • Move common info from register to the module level (a lot of this was duplicated -- less maintainable and harder to read)
  • Use interpreter shutdown consistently, introducing it as a more general term for the old docs' "program termination".
  • Use consistent terminology (exit handlers)
  • Move warning for a mitigated footgun to a change entry
  • Add a new warning about keeping things usable

Similarly clarify docs for the atexit attribute in weakref.

For the `atexit` module:

- Move common info from `register` to the module level (a  lot of this
  was duplicated -- less maintainable and harder to read)
- Use *interpreter shutdown* consistently, introducing it as a more
  general term for the old docs' "program termination".
- Use consistent terminology (exit handlers)
- Move warning for a mitigated footgun to a change entry
- Add a new warning about keeping things usable

Similarly clarify docs for the `atexit` attribute in `weakref`.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34150483 | 📁 Comparing 1354c91 against main (5be8020)

  🔍 Preview build  

2 files changed
± library/atexit.html
± library/weakref.html

Comment thread Doc/library/atexit.rst
Comment on lines 38 to 39
**Note:** The effect of registering or unregistering functions from within
a cleanup function is undefined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is defined starting in 3.15; Python will run any handlers that have been added.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, really?

import atexit
atexit.register(print, 'run me!')
@atexit.register
def foo():
    atexit.register(print, 'run me too!')
$ python3.15 /tmp/demo.py 
run me!

In any case, should we guarantee that? What about unregistering?

Sounds like material for a follow-up that's not backported to 3.14.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, you may have found a bug. atexit callbacks are supposed to be ran in a loop:

interp_has_atexit_callbacks(PyInterpreterState *interp)

make_pre_finalization_calls(PyThreadState *tstate, int subinterpreters)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it works as documented -- and better than an infinite loop with a function registering itself...

Comment thread Doc/library/atexit.rst
Attempts to start a new thread or :func:`os.fork` a new process
in an exit handler now leads to :exc:`RuntimeError`.
Previously, this could cause race conditions between the main Python
runtime thread freeing thread states while internal :mod:`threading`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link to the term here?

Suggested change
runtime thread freeing thread states while internal :mod:`threading`
runtime thread freeing :term:`thread states <thread state>` while internal :mod:`threading`

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarifying which internals exactly cause your crash isn't too useful.
(I considered removing the details entirely, but kept them to illustrate the warning above.)

@kumaraditya303
kumaraditya303 removed their request for review August 21, 2026 16:15
@encukou encukou added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Aug 26, 2026
@encukou

encukou commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

I'll merge ~Friday if there are no objections.

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or merge now :)

@encukou
encukou merged commit 1d28836 into python:main Sep 3, 2026
48 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @encukou for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Sep 3, 2026
@bedevere-app

bedevere-app Bot commented Sep 3, 2026

Copy link
Copy Markdown

GH-156878 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 3, 2026
@miss-islington-app

Copy link
Copy Markdown

Sorry, @encukou, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1d28836e43efba08ac71d91d135d721e1f4ada69 3.13

@bedevere-app

bedevere-app Bot commented Sep 3, 2026

Copy link
Copy Markdown

GH-156879 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 3, 2026
@bedevere-app

bedevere-app Bot commented Sep 3, 2026

Copy link
Copy Markdown

GH-156889 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Sep 3, 2026
encukou added a commit that referenced this pull request Sep 3, 2026
For the `atexit` module:

- Move common info from `register` to the module level (a lot of this
  was duplicated -- less maintainable and harder to read)
- Use *interpreter shutdown* consistently, introducing it as a more
  general term for the old docs' *program termination*.
- Use the term *exit handler* consistently
- Move warning for a mitigated footgun to a change entry
- Add a new warning about keeping things usable

Similarly clarify docs for the `atexit` attribute in `weakref`.
(cherry picked from commit 1d28836)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
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 skip issue skip news

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants