Skip to content

Commit 776407f

Browse files
Marcel Plchpitrou
authored andcommitted
bpo-31901: atexit callbacks should be run at subinterpreter shutdown (#4611)
Change atexit behavior and PEP-489 multiphase init support.
1 parent 1976086 commit 776407f

File tree

10 files changed

+91
-46
lines changed

10 files changed

+91
-46
lines changed

Doc/library/atexit.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ at interpreter termination time they will be run in the order ``C``, ``B``,
2020
program is killed by a signal not handled by Python, when a Python fatal
2121
internal error is detected, or when :func:`os._exit` is called.
2222

23+
.. versionchanged:: 3.7
24+
When used with C-API subinterpreters, registered functions
25+
are local to the interpreter they were registered in.
2326

2427
.. function:: register(func, *args, **kwargs)
2528

Include/internal/pystate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ typedef struct pyruntimestate {
9090
#define NEXITFUNCS 32
9191
void (*exitfuncs[NEXITFUNCS])(void);
9292
int nexitfuncs;
93-
void (*pyexitfunc)(void);
9493

9594
struct _gc_runtime_state gc;
9695
struct _warnings_runtime_state warnings;

Include/pylifecycle.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ PyAPI_FUNC(void) Py_EndInterpreter(PyThreadState *);
9292
* exit functions.
9393
*/
9494
#ifndef Py_LIMITED_API
95-
PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(void));
95+
PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(PyObject *), PyObject *);
9696
#endif
9797
PyAPI_FUNC(int) Py_AtExit(void (*func)(void));
9898

Include/pystate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ typedef struct _is {
131131
PyObject *after_forkers_parent;
132132
PyObject *after_forkers_child;
133133
#endif
134+
/* AtExit module */
135+
void (*pyexitfunc)(PyObject *);
136+
PyObject *pyexitmodule;
134137
} PyInterpreterState;
135138
#endif /* !Py_LIMITED_API */
136139

Lib/test/test_atexit.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import io
44
import atexit
5+
import os
56
from test import support
67
from test.support import script_helper
78

@@ -203,6 +204,24 @@ def f():
203204
self.assertEqual(ret, 0)
204205
self.assertEqual(atexit._ncallbacks(), n)
205206

207+
def test_callback_on_subinterpreter_teardown(self):
208+
# This tests if a callback is called on
209+
# subinterpreter teardown.
210+
expected = b"The test has passed!"
211+
r, w = os.pipe()
212+
213+
code = r"""if 1:
214+
import os
215+
import atexit
216+
def callback():
217+
os.write({:d}, b"The test has passed!")
218+
atexit.register(callback)
219+
""".format(w)
220+
ret = support.run_in_subinterp(code)
221+
os.close(w)
222+
self.assertEqual(os.read(r, len(expected)), expected)
223+
os.close(r)
224+
206225

207226
if __name__ == "__main__":
208227
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The `atexit` module now has its callback stored per interpreter.

Modules/atexitmodule.c

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ atexit_cleanup(atexitmodule_state *modstate)
6363
/* Installed into pylifecycle.c's atexit mechanism */
6464

6565
static void
66-
atexit_callfuncs(void)
66+
atexit_callfuncs(PyObject *module)
6767
{
6868
PyObject *exc_type = NULL, *exc_value, *exc_tb, *r;
6969
atexit_callback *cb;
70-
PyObject *module;
7170
atexitmodule_state *modstate;
7271
int i;
7372

74-
module = PyState_FindModule(&atexitmodule);
7573
if (module == NULL)
7674
return;
7775
modstate = GET_ATEXIT_STATE(module);
@@ -185,7 +183,7 @@ Run all registered exit functions.");
185183
static PyObject *
186184
atexit_run_exitfuncs(PyObject *self, PyObject *unused)
187185
{
188-
atexit_callfuncs();
186+
atexit_callfuncs(self);
189187
if (PyErr_Occurred())
190188
return NULL;
191189
Py_RETURN_NONE;
@@ -225,13 +223,15 @@ atexit_m_traverse(PyObject *self, visitproc visit, void *arg)
225223
atexitmodule_state *modstate;
226224

227225
modstate = GET_ATEXIT_STATE(self);
228-
for (i = 0; i < modstate->ncallbacks; i++) {
229-
atexit_callback *cb = modstate->atexit_callbacks[i];
230-
if (cb == NULL)
231-
continue;
232-
Py_VISIT(cb->func);
233-
Py_VISIT(cb->args);
234-
Py_VISIT(cb->kwargs);
226+
if (modstate != NULL) {
227+
for (i = 0; i < modstate->ncallbacks; i++) {
228+
atexit_callback *cb = modstate->atexit_callbacks[i];
229+
if (cb == NULL)
230+
continue;
231+
Py_VISIT(cb->func);
232+
Py_VISIT(cb->args);
233+
Py_VISIT(cb->kwargs);
234+
}
235235
}
236236
return 0;
237237
}
@@ -241,7 +241,9 @@ atexit_m_clear(PyObject *self)
241241
{
242242
atexitmodule_state *modstate;
243243
modstate = GET_ATEXIT_STATE(self);
244-
atexit_cleanup(modstate);
244+
if (modstate != NULL) {
245+
atexit_cleanup(modstate);
246+
}
245247
return 0;
246248
}
247249

@@ -250,8 +252,10 @@ atexit_free(PyObject *m)
250252
{
251253
atexitmodule_state *modstate;
252254
modstate = GET_ATEXIT_STATE(m);
253-
atexit_cleanup(modstate);
254-
PyMem_Free(modstate->atexit_callbacks);
255+
if (modstate != NULL) {
256+
atexit_cleanup(modstate);
257+
PyMem_Free(modstate->atexit_callbacks);
258+
}
255259
}
256260

257261
PyDoc_STRVAR(atexit_unregister__doc__,
@@ -310,14 +314,34 @@ upon normal program termination.\n\
310314
Two public functions, register and unregister, are defined.\n\
311315
");
312316

317+
static int
318+
atexit_exec(PyObject *m) {
319+
atexitmodule_state *modstate;
320+
321+
modstate = GET_ATEXIT_STATE(m);
322+
modstate->callback_len = 32;
323+
modstate->ncallbacks = 0;
324+
modstate->atexit_callbacks = PyMem_New(atexit_callback*,
325+
modstate->callback_len);
326+
if (modstate->atexit_callbacks == NULL)
327+
return -1;
328+
329+
_Py_PyAtExit(atexit_callfuncs, m);
330+
return 0;
331+
}
332+
333+
static PyModuleDef_Slot atexit_slots[] = {
334+
{Py_mod_exec, atexit_exec},
335+
{0, NULL}
336+
};
313337

314338
static struct PyModuleDef atexitmodule = {
315339
PyModuleDef_HEAD_INIT,
316340
"atexit",
317341
atexit__doc__,
318342
sizeof(atexitmodule_state),
319343
atexit_methods,
320-
NULL,
344+
atexit_slots,
321345
atexit_m_traverse,
322346
atexit_m_clear,
323347
(freefunc)atexit_free
@@ -326,21 +350,5 @@ static struct PyModuleDef atexitmodule = {
326350
PyMODINIT_FUNC
327351
PyInit_atexit(void)
328352
{
329-
PyObject *m;
330-
atexitmodule_state *modstate;
331-
332-
m = PyModule_Create(&atexitmodule);
333-
if (m == NULL)
334-
return NULL;
335-
336-
modstate = GET_ATEXIT_STATE(m);
337-
modstate->callback_len = 32;
338-
modstate->ncallbacks = 0;
339-
modstate->atexit_callbacks = PyMem_New(atexit_callback*,
340-
modstate->callback_len);
341-
if (modstate->atexit_callbacks == NULL)
342-
return NULL;
343-
344-
_Py_PyAtExit(atexit_callfuncs);
345-
return m;
353+
return PyModuleDef_Init(&atexitmodule);
346354
}

Programs/_testembed.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ static int test_forced_io_encoding(void)
126126
return 0;
127127
}
128128

129-
130129
/*********************************************************
131130
* Test parts of the C-API that work before initialization
132131
*********************************************************/

Python/pylifecycle.c

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static _PyInitError initfsencoding(PyInterpreterState *interp);
5656
static _PyInitError initsite(void);
5757
static _PyInitError init_sys_streams(PyInterpreterState *interp);
5858
static _PyInitError initsigs(void);
59-
static void call_py_exitfuncs(void);
59+
static void call_py_exitfuncs(PyInterpreterState *);
6060
static void wait_for_thread_shutdown(void);
6161
static void call_ll_exitfuncs(void);
6262
extern int _PyUnicode_Init(void);
@@ -1006,6 +1006,10 @@ Py_FinalizeEx(void)
10061006

10071007
wait_for_thread_shutdown();
10081008

1009+
/* Get current thread state and interpreter pointer */
1010+
tstate = PyThreadState_GET();
1011+
interp = tstate->interp;
1012+
10091013
/* The interpreter is still entirely intact at this point, and the
10101014
* exit funcs may be relying on that. In particular, if some thread
10111015
* or exit func is still waiting to do an import, the import machinery
@@ -1015,11 +1019,8 @@ Py_FinalizeEx(void)
10151019
* threads created thru it, so this also protects pending imports in
10161020
* the threads created via Threading.
10171021
*/
1018-
call_py_exitfuncs();
10191022

1020-
/* Get current thread state and interpreter pointer */
1021-
tstate = PyThreadState_GET();
1022-
interp = tstate->interp;
1023+
call_py_exitfuncs(interp);
10231024

10241025
/* Copy the core config, PyInterpreterState_Delete() free
10251026
the core config memory */
@@ -1412,6 +1413,8 @@ Py_EndInterpreter(PyThreadState *tstate)
14121413

14131414
wait_for_thread_shutdown();
14141415

1416+
call_py_exitfuncs(interp);
1417+
14151418
if (tstate != interp->tstate_head || tstate->next != NULL)
14161419
Py_FatalError("Py_EndInterpreter: not the last thread");
14171420

@@ -2023,20 +2026,28 @@ _Py_FatalInitError(_PyInitError err)
20232026
# include "pythread.h"
20242027

20252028
/* For the atexit module. */
2026-
void _Py_PyAtExit(void (*func)(void))
2029+
void _Py_PyAtExit(void (*func)(PyObject *), PyObject *module)
20272030
{
2031+
PyThreadState *ts;
2032+
PyInterpreterState *is;
2033+
2034+
ts = PyThreadState_GET();
2035+
is = ts->interp;
2036+
20282037
/* Guard against API misuse (see bpo-17852) */
2029-
assert(_PyRuntime.pyexitfunc == NULL || _PyRuntime.pyexitfunc == func);
2030-
_PyRuntime.pyexitfunc = func;
2038+
assert(is->pyexitfunc == NULL || is->pyexitfunc == func);
2039+
2040+
is->pyexitfunc = func;
2041+
is->pyexitmodule = module;
20312042
}
20322043

20332044
static void
2034-
call_py_exitfuncs(void)
2045+
call_py_exitfuncs(PyInterpreterState *istate)
20352046
{
2036-
if (_PyRuntime.pyexitfunc == NULL)
2047+
if (istate->pyexitfunc == NULL)
20372048
return;
20382049

2039-
(*_PyRuntime.pyexitfunc)();
2050+
(*istate->pyexitfunc)(istate->pyexitmodule);
20402051
PyErr_Clear();
20412052
}
20422053

Python/pystate.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ PyInterpreterState_New(void)
153153
interp->after_forkers_parent = NULL;
154154
interp->after_forkers_child = NULL;
155155
#endif
156+
interp->pyexitfunc = NULL;
157+
interp->pyexitmodule = NULL;
156158

157159
HEAD_LOCK();
158160
interp->next = _PyRuntime.interpreters.head;

0 commit comments

Comments
 (0)