classification
Title: atexit: guarantee order of execution of registered functions?
Type: Stage: resolved
Components: Documentation Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: benjamin.peterson, docs@python, eric.araujo, georg.brandl, giampaolo.rodola, larry, loewis, meador.inge, neologix, pitrou, python-dev, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2012-07-01 07:58 by georg.brandl, last changed 2013-08-21 16:47 by neologix. This issue is now closed.

Files
File name Uploaded Description Edit
larry.atexit.1.diff larry, 2012-07-02 14:24 First patch, just doc changes. review
Messages (23)
msg164454 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-07-01 07:58
Currently, the atexit docs state

"The order in which the functions are called is not defined" in the introduction and

"all functions registered are called in last in, first out order" in the docs of the atexit() function.

While the latter is correct, I don't think we should guarantee that behaviour by documenting it.  Suggestions?
msg164458 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-07-01 08:20
I believe it should be guaranteed behavior and that code may reasonably want to have a predictable sequence of unwinding behaviors.
msg164471 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-07-01 10:17
It is guaranteed by libc's atexit(3), and should also be guaranteed here.
msg164472 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-01 10:20
+1 to guaranteeing the order.
msg164475 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-01 11:27
> I believe it should be guaranteed behavior and that code may
> reasonably want to have a predictable sequence of unwinding behaviors.

Agreed.
msg164516 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-02 14:24
Here's a patch.  I read the implementation to find out what it does, then documented it.  Happily it does the sane thing.
msg164517 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-02 14:30
> Here's a patch.  I read the implementation to find out what it does,
> then documented it.  Happily it does the sane thing.

Is it tested for?
msg164527 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-07-02 19:10
See #9788 for previous discussion.
msg164555 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-07-02 21:34
The patch wording is fine.  However, I would avoid the "note" markup.  It is visually distracting and there isn't a reason to pull this out of the main text.
msg164579 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-03 06:26
> Is it tested for?

Amusingly enough, yes.  test_order() in Lib/test/test_atexit.py already ensures reverse order.
msg164581 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-03 06:51
> I would avoid the "note" markup.  It is visually distracting
> and there isn't a reason to pull this out of the main text.

I think it's an improvement; it helps draw the eye to an important warning.

Does anyone else have an opinion one way or another?
msg164588 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-03 09:23
> Does anyone else have an opinion one way or another?

I'm neutral.
msg164753 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-07-06 20:11
I agree with Raymond. You might, however, emphasize 'normal' in 'normal
interpreter termination'. That is the key point. The 'note' merely explains 'abnormal'.
msg165482 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-15 01:16
Okay, I'll remove the ".. note".  Checking in shortly.
msg165483 - (view) Author: Roundup Robot (python-dev) Date: 2012-07-15 01:21
New changeset f8b520b6f654 by Larry Hastings in branch 'default':
- Issue #15233: Python now guarantees that callables registered with
http://hg.python.org/cpython/rev/f8b520b6f654
msg165484 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-15 01:22
So, it's checked in to trunk.  Shall I also backport to 2.7 and 3.2 as the issue suggests?
msg165535 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2012-07-15 16:55
> Shall I also backport to 2.7 and 3.2 as the issue suggests?

If the tests are there, yes!
msg165567 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-07-16 04:56
I have been meditating on this, and I'm not sure we should change 2.7.  3.2 might be okay.

The thing is, I fear we're not just talking about CPython implementation details, we're talking about the Python Standard Library.  The existing documentation clearly gives alternative implementations free license to call atexit registered functions in any order they like.  So it's conceivable that other implementations don't guarantee reverse order.

I don't know what the right thing is to do here.  It seems like we could define atexit's behavior for 2.7 *if* all the major alternative implementations happily also implicitly guarantee reverse order.  Failing that we probably shouldn't touch it.  Or we could just document that reverse order is guaranteed in CPython only.

Of course, there are no alternative implementations that support 3.2.  That's why I think 3.2 might be okay.  It does seem strange to redefine part of the implicit language spec more than a year after the release shipped though.
msg165576 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2012-07-16 07:01
+1 for backport.  There's no benefit to leaving ambiguity floating about, nor was there ever any intention for the behavior to have been different.
msg172599 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-10-10 19:31
Ping.
Like Raymond, I think there's no reason to leave ambiguity in 2.7 and 3.2,  so this doc patch is probably worth backporting.
msg194258 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2013-08-03 13:31
Unless anyone objects, I'll backport it soonish.
msg195803 - (view) Author: Roundup Robot (python-dev) Date: 2013-08-21 16:27
New changeset 84d74eb7a341 by Charles-François Natali in branch '2.7':
Issue #15233: Python now guarantees that callables registered with the atexit
http://hg.python.org/cpython/rev/84d74eb7a341
msg195805 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2013-08-21 16:47
Backported to 2.7.
History
Date User Action Args
2013-08-21 16:47:44neologixsetstatus: open -> closed
versions: - Python 3.2
messages: + msg195805

resolution: fixed
stage: commit review -> resolved
2013-08-21 16:27:53python-devsetmessages: + msg195803
2013-08-03 13:31:31neologixsetmessages: + msg194258
stage: commit review
2012-10-10 19:31:11neologixsetmessages: + msg172599
2012-07-16 07:01:15rhettingersetmessages: + msg165576
2012-07-16 04:56:26larrysetmessages: + msg165567
2012-07-15 16:55:57pitrousetmessages: + msg165535
2012-07-15 01:22:22larrysetmessages: + msg165484
2012-07-15 01:21:15python-devsetmessages: + msg165483
2012-07-15 01:16:12larrysetmessages: + msg165482
2012-07-08 04:38:23eric.araujosetnosy: + giampaolo.rodola, benjamin.peterson, meador.inge, python-dev
2012-07-06 20:11:59terry.reedysetnosy: + terry.reedy
messages: + msg164753
2012-07-03 09:23:24pitrousetmessages: + msg164588
2012-07-03 06:51:05larrysetmessages: + msg164581
2012-07-03 06:26:19larrysetmessages: + msg164579
2012-07-02 21:34:36rhettingersetmessages: + msg164555
2012-07-02 19:10:52eric.araujosetnosy: + eric.araujo
messages: + msg164527
2012-07-02 14:30:21pitrousetmessages: + msg164517
2012-07-02 14:24:41larrysetfiles: + larry.atexit.1.diff
keywords: + patch
messages: + msg164516
2012-07-01 11:27:14pitrousetmessages: + msg164475
2012-07-01 10:20:24larrysetnosy: + larry
messages: + msg164472
2012-07-01 10:17:29neologixsetnosy: + neologix
messages: + msg164471
2012-07-01 08:20:48rhettingersetnosy: + rhettinger
messages: + msg164458
2012-07-01 07:58:41georg.brandlcreate