summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-06-08 18:08:28 (GMT)
committerGitHub <noreply@github.com>2023-06-08 18:08:28 (GMT)
commit7799c8e678f759c7787785c6287140abe641d1b9 (patch)
treee24087a6bac775c13bdb0846a88d51e892ed58a9 /Python/pylifecycle.c
parent6a8b8623574b6d07ee6caf4d007c4778a7c2d372 (diff)
downloadcpython-7799c8e678f759c7787785c6287140abe641d1b9.zip
cpython-7799c8e678f759c7787785c6287140abe641d1b9.tar.gz
cpython-7799c8e678f759c7787785c6287140abe641d1b9.tar.bz2
gh-100227: Lock Around Use of the Global "atexit" State (gh-105514)
The risk of a race with this state is relatively low, but we play it safe anyway.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c17
1 files changed, 14 insertions, 3 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index 4c21160..9ac5630 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -2973,24 +2973,35 @@ wait_for_thread_shutdown(PyThreadState *tstate)
int Py_AtExit(void (*func)(void))
{
- if (_PyRuntime.atexit.ncallbacks >= NEXITFUNCS)
+ struct _atexit_runtime_state *state = &_PyRuntime.atexit;
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
+ if (state->ncallbacks >= NEXITFUNCS) {
+ PyThread_release_lock(state->mutex);
return -1;
- _PyRuntime.atexit.callbacks[_PyRuntime.atexit.ncallbacks++] = func;
+ }
+ state->callbacks[state->ncallbacks++] = func;
+ PyThread_release_lock(state->mutex);
return 0;
}
static void
call_ll_exitfuncs(_PyRuntimeState *runtime)
{
+ atexit_callbackfunc exitfunc;
struct _atexit_runtime_state *state = &runtime->atexit;
+
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
while (state->ncallbacks > 0) {
/* pop last function from the list */
state->ncallbacks--;
- atexit_callbackfunc exitfunc = state->callbacks[state->ncallbacks];
+ exitfunc = state->callbacks[state->ncallbacks];
state->callbacks[state->ncallbacks] = NULL;
+ PyThread_release_lock(state->mutex);
exitfunc();
+ PyThread_acquire_lock(state->mutex, WAIT_LOCK);
}
+ PyThread_release_lock(state->mutex);
fflush(stdout);
fflush(stderr);