Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-44348: BaseException deallocator uses trashcan #28190

Merged
merged 2 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
The deallocator function of the :exc:`BaseException` type now uses the
trashcan mecanism to prevent stack overflow. For example, when a
:exc:`RecursionError` instance is raised, it can be linked to another
RecursionError through the ``__context__`` attribute or the
``__traceback__`` attribute, and then a chain of exceptions is created. When
the chain is destroyed, nested deallocator function calls can crash with a
stack overflow if the chain is too long compared to the available stack
memory. Patch by Victor Stinner.
7 changes: 6 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,14 @@ BaseException_clear(PyBaseExceptionObject *self)
static void
BaseException_dealloc(PyBaseExceptionObject *self)
{
_PyObject_GC_UNTRACK(self);
PyObject_GC_UnTrack(self);
// bpo-44348: The trashcan mecanism prevents stack overflow when deleting
// long chains of exceptions. For example, exceptions can be chained
// through the __context__ attributes or the __traceback__ attribute.
Py_TRASHCAN_BEGIN(self, BaseException_dealloc)
BaseException_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
Py_TRASHCAN_END
}

static int
Expand Down