]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46025: Fix a crash in the atexit module for auto-unregistering functions (GH...
authorPablo Galindo Salgado <Pablogsal@gmail.com>
Thu, 9 Dec 2021 13:53:44 +0000 (13:53 +0000)
committerGitHub <noreply@github.com>
Thu, 9 Dec 2021 13:53:44 +0000 (13:53 +0000)
Lib/test/_test_atexit.py
Misc/NEWS.d/next/Core and Builtins/2021-12-09-11-41-35.bpo-46025.pkEvW9.rst [new file with mode: 0644]
Modules/atexitmodule.c

index a31658531113ba037ec7dd4e6aaf68c7c6389ee0..55d28083349175bc7e8a0b718bfa08813fb7f5e2 100644 (file)
@@ -116,6 +116,21 @@ class GeneralTest(unittest.TestCase):
         atexit._run_exitfuncs()
         self.assertEqual(l, [5])
 
+    def test_atexit_with_unregistered_function(self):
+        # See bpo-46025 for more info
+        def func():
+            atexit.unregister(func)
+            1/0
+        atexit.register(func)
+        try:
+            with support.catch_unraisable_exception() as cm:
+                atexit._run_exitfuncs()
+                self.assertEqual(cm.unraisable.object, func)
+                self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError)
+                self.assertEqual(type(cm.unraisable.exc_value), ZeroDivisionError)
+        finally:
+            atexit.unregister(func)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-12-09-11-41-35.bpo-46025.pkEvW9.rst b/Misc/NEWS.d/next/Core and Builtins/2021-12-09-11-41-35.bpo-46025.pkEvW9.rst
new file mode 100644 (file)
index 0000000..dd2f1ff
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash in the :mod:`atexit` module involving functions that unregister
+themselves before raising exceptions. Patch by Pablo Galindo.
index e536b4abe295f00fd21f5e028f6d4370d7b7d893..95c653cf4782a23c1cdd5f01346610aa5d607622 100644 (file)
@@ -93,13 +93,16 @@ atexit_callfuncs(struct atexit_state *state)
             continue;
         }
 
+        // bpo-46025: Increment the refcount of cb->func as the call itself may unregister it
+        PyObject* the_func = Py_NewRef(cb->func);
         PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs);
         if (res == NULL) {
-            _PyErr_WriteUnraisableMsg("in atexit callback", cb->func);
+            _PyErr_WriteUnraisableMsg("in atexit callback", the_func);
         }
         else {
             Py_DECREF(res);
         }
+        Py_DECREF(the_func);
     }
 
     atexit_cleanup(state);