Skip to content

Commit a66f9c6

Browse files
authored
bpo-30341: Improve _PyTrash_thread_destroy_chain() a little bit (#1545)
* add a comment about why we need to increase trash_delete_nesting * move increase and decrese outside of the loop
1 parent 8619c54 commit a66f9c6

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ without deallocating anything (and so unbounded call-stack depth is avoided).
10291029
When the call stack finishes unwinding again, code generated by the END macro
10301030
notices this, and calls another routine to deallocate all the objects that
10311031
may have been added to the list of deferred deallocations. In effect, a
1032-
chain of N deallocations is broken into N / PyTrash_UNWIND_LEVEL pieces,
1032+
chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces,
10331033
with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
10341034
*/
10351035

Objects/object.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,19 @@ void
20932093
_PyTrash_thread_destroy_chain(void)
20942094
{
20952095
PyThreadState *tstate = PyThreadState_GET();
2096+
/* We need to increase trash_delete_nesting here, otherwise,
2097+
_PyTrash_thread_destroy_chain will be called recursively
2098+
and then possibly crash. An example that may crash without
2099+
increase:
2100+
N = 500000 # need to be large enough
2101+
ob = object()
2102+
tups = [(ob,) for i in range(N)]
2103+
for i in range(49):
2104+
tups = [(tup,) for tup in tups]
2105+
del tups
2106+
*/
2107+
assert(tstate->trash_delete_nesting == 0);
2108+
++tstate->trash_delete_nesting;
20962109
while (tstate->trash_delete_later) {
20972110
PyObject *op = tstate->trash_delete_later;
20982111
destructor dealloc = Py_TYPE(op)->tp_dealloc;
@@ -2107,10 +2120,10 @@ _PyTrash_thread_destroy_chain(void)
21072120
* up distorting allocation statistics.
21082121
*/
21092122
assert(op->ob_refcnt == 0);
2110-
++tstate->trash_delete_nesting;
21112123
(*dealloc)(op);
2112-
--tstate->trash_delete_nesting;
2124+
assert(tstate->trash_delete_nesting == 1);
21132125
}
2126+
--tstate->trash_delete_nesting;
21142127
}
21152128

21162129
#ifndef Py_TRACE_REFS

0 commit comments

Comments
 (0)