Skip to content

Commit f0e04b2

Browse files
[3.6] bpo-33622: Fix issues with handling errors in the GC. (GH-7078) (GH-7095)
* Fixed a leak when the GC fails to add an object with __del__ into the gc.garbage list. * PyGC_Collect() can now be called when an exception is set and preserves it. * Fixed an undefined behavior with comparing a dead pointer with NULL. (cherry picked from commit 301e3cc)
1 parent 935c81b commit f0e04b2

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fixed a leak when the garbage collector fails to add an object with the
2+
``__del__`` method or referenced by it into the :data:`gc.garbage` list.
3+
:c:func:`PyGC_Collect` can now be called when an exception is set and
4+
preserves it.

Modules/gcmodule.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,8 @@ debug_cycle(const char *msg, PyObject *op)
751751
* garbage list (a Python list), else only the objects in finalizers with
752752
* __del__ methods are appended to garbage. All objects in finalizers are
753753
* merged into the old list regardless.
754-
* Returns 0 if all OK, <0 on error (out of memory to grow the garbage list).
755-
* The finalizers list is made empty on a successful return.
756754
*/
757-
static int
755+
static void
758756
handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
759757
{
760758
PyGC_Head *gc = finalizers->gc.gc_next;
@@ -769,12 +767,11 @@ handle_legacy_finalizers(PyGC_Head *finalizers, PyGC_Head *old)
769767

770768
if ((debug & DEBUG_SAVEALL) || has_legacy_finalizer(op)) {
771769
if (PyList_Append(garbage, op) < 0)
772-
return -1;
770+
break;
773771
}
774772
}
775773

776774
gc_list_merge(finalizers, old);
777-
return 0;
778775
}
779776

780777
/* Run first-time finalizers (if any) on all the objects in collectable.
@@ -1045,7 +1042,7 @@ collect(int generation, Py_ssize_t *n_collected, Py_ssize_t *n_uncollectable,
10451042
* reachable list of garbage. The programmer has to deal with
10461043
* this if they insist on creating this type of structure.
10471044
*/
1048-
(void)handle_legacy_finalizers(&finalizers, old);
1045+
handle_legacy_finalizers(&finalizers, old);
10491046

10501047
/* Clear free list only during the collection of the highest
10511048
* generation */
@@ -1109,9 +1106,12 @@ invoke_gc_callback(const char *phase, int generation,
11091106
PyObject *r, *cb = PyList_GET_ITEM(callbacks, i);
11101107
Py_INCREF(cb); /* make sure cb doesn't go away */
11111108
r = PyObject_CallFunction(cb, "sO", phase, info);
1112-
Py_XDECREF(r);
1113-
if (r == NULL)
1109+
if (r == NULL) {
11141110
PyErr_WriteUnraisable(cb);
1111+
}
1112+
else {
1113+
Py_DECREF(r);
1114+
}
11151115
Py_DECREF(cb);
11161116
}
11171117
Py_XDECREF(info);
@@ -1588,8 +1588,11 @@ PyGC_Collect(void)
15881588
if (collecting)
15891589
n = 0; /* already collecting, don't do anything */
15901590
else {
1591+
PyObject *exc, *value, *tb;
15911592
collecting = 1;
1593+
PyErr_Fetch(&exc, &value, &tb);
15921594
n = collect_with_callback(NUM_GENERATIONS - 1);
1595+
PyErr_Restore(exc, value, tb);
15931596
collecting = 0;
15941597
}
15951598

0 commit comments

Comments
 (0)