Skip to content

Commit 656c45e

Browse files
authored
bpo-38631: Avoid Py_FatalError() in GC collect() (GH-18164)
collect() should not get an exception, but it does, logging the exception is enough. Override sys.unraisablehook to decide how to handle unraisable exceptions. Py_FatalError() should be avoided whenever possible.
1 parent 8870433 commit 656c45e

File tree

1 file changed

+1
-7
lines changed

1 file changed

+1
-7
lines changed

Modules/gcmodule.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ gc_decref(PyGC_Head *g)
118118
g->_gc_prev -= 1 << _PyGC_PREV_SHIFT;
119119
}
120120

121-
/* Python string to use if unhandled exception occurs */
122-
static PyObject *gc_str = NULL;
123-
124121
/* set for debugging information */
125122
#define DEBUG_STATS (1<<0) /* print collection statistics */
126123
#define DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
@@ -1310,10 +1307,7 @@ collect(PyThreadState *tstate, int generation,
13101307
_PyErr_Clear(tstate);
13111308
}
13121309
else {
1313-
if (gc_str == NULL)
1314-
gc_str = PyUnicode_FromString("garbage collection");
1315-
PyErr_WriteUnraisable(gc_str);
1316-
Py_FatalError("unexpected exception during garbage collection");
1310+
_PyErr_WriteUnraisableMsg("in garbage collection", NULL);
13171311
}
13181312
}
13191313

0 commit comments

Comments
 (0)