Skip to content

Commit d54bca9

Browse files
committed
Fix race with unregistration.
1 parent b8e4ae8 commit d54bca9

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

Modules/atexitmodule.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,21 @@ atexit_unregister(PyObject *module, PyObject *func)
311311
if (cb == NULL) {
312312
continue;
313313
}
314-
315-
// We need to hold our own reference to this
316-
// in case another thread is trying to unregister as well.
317314
PyObject *to_compare = cb->func;
315+
316+
// Unlock for fear of a custom __eq__ causing re-entrancy
318317
_PyAtExit_UNLOCK(state);
319318
int eq = PyObject_RichCompareBool(to_compare, func, Py_EQ);
320319
if (eq < 0) {
321320
return NULL;
322321
}
323322
_PyAtExit_LOCK(state);
323+
if (state->callbacks[i] == NULL)
324+
{
325+
// Edge case: another thread might have
326+
// unregistered the function while we released the lock.
327+
continue;
328+
}
324329
if (eq) {
325330
atexit_delete_cb(state, i);
326331
}

0 commit comments

Comments
 (0)