|
| 1 | +From fa787ef1e88965aedd927258ae8aad59fd81abc7 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Pablo Galindo Salgado <Pablogsal@gmail.com> |
| 3 | +Date: Thu, 9 Dec 2021 13:53:44 +0000 |
| 4 | +Subject: [PATCH] bpo-46025: Fix a crash in the atexit module for |
| 5 | + auto-unregistering functions (GH-30002) (cherry picked from commit |
| 6 | + f0d290d25cad66e615ada68ba190b8a23ac1deaa) |
| 7 | + |
| 8 | +Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com> |
| 9 | +--- |
| 10 | + Lib/test/_test_atexit.py | 15 +++++++++++++++ |
| 11 | + .../2021-12-09-11-41-35.bpo-46025.pkEvW9.rst | 2 ++ |
| 12 | + Modules/atexitmodule.c | 5 ++++- |
| 13 | + 3 files changed, 21 insertions(+), 1 deletion(-) |
| 14 | + create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-12-09-11-41-35.bpo-46025.pkEvW9.rst |
| 15 | + |
| 16 | +diff --git a/Lib/test/_test_atexit.py b/Lib/test/_test_atexit.py |
| 17 | +index a31658531113b..55d2808334917 100644 |
| 18 | +--- a/Lib/test/_test_atexit.py |
| 19 | ++++ b/Lib/test/_test_atexit.py |
| 20 | +@@ -116,6 +116,21 @@ def test_bound_methods(self): |
| 21 | + atexit._run_exitfuncs() |
| 22 | + self.assertEqual(l, [5]) |
| 23 | + |
| 24 | ++ def test_atexit_with_unregistered_function(self): |
| 25 | ++ # See bpo-46025 for more info |
| 26 | ++ def func(): |
| 27 | ++ atexit.unregister(func) |
| 28 | ++ 1/0 |
| 29 | ++ atexit.register(func) |
| 30 | ++ try: |
| 31 | ++ with support.catch_unraisable_exception() as cm: |
| 32 | ++ atexit._run_exitfuncs() |
| 33 | ++ self.assertEqual(cm.unraisable.object, func) |
| 34 | ++ self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError) |
| 35 | ++ self.assertEqual(type(cm.unraisable.exc_value), ZeroDivisionError) |
| 36 | ++ finally: |
| 37 | ++ atexit.unregister(func) |
| 38 | ++ |
| 39 | + |
| 40 | + if __name__ == "__main__": |
| 41 | + unittest.main() |
| 42 | +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 |
| 43 | +new file mode 100644 |
| 44 | +index 0000000000000..dd2f1ff4731e7 |
| 45 | +--- /dev/null |
| 46 | ++++ b/Misc/NEWS.d/next/Core and Builtins/2021-12-09-11-41-35.bpo-46025.pkEvW9.rst |
| 47 | +@@ -0,0 +1,2 @@ |
| 48 | ++Fix a crash in the :mod:`atexit` module involving functions that unregister |
| 49 | ++themselves before raising exceptions. Patch by Pablo Galindo. |
| 50 | +diff --git a/Modules/atexitmodule.c b/Modules/atexitmodule.c |
| 51 | +index e536b4abe295f..95c653cf4782a 100644 |
| 52 | +--- a/Modules/atexitmodule.c |
| 53 | ++++ b/Modules/atexitmodule.c |
| 54 | +@@ -93,13 +93,16 @@ atexit_callfuncs(struct atexit_state *state) |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | ++ // bpo-46025: Increment the refcount of cb->func as the call itself may unregister it |
| 59 | ++ PyObject* the_func = Py_NewRef(cb->func); |
| 60 | + PyObject *res = PyObject_Call(cb->func, cb->args, cb->kwargs); |
| 61 | + if (res == NULL) { |
| 62 | +- _PyErr_WriteUnraisableMsg("in atexit callback", cb->func); |
| 63 | ++ _PyErr_WriteUnraisableMsg("in atexit callback", the_func); |
| 64 | + } |
| 65 | + else { |
| 66 | + Py_DECREF(res); |
| 67 | + } |
| 68 | ++ Py_DECREF(the_func); |
| 69 | + } |
| 70 | + |
| 71 | + atexit_cleanup(state); |
0 commit comments