File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -1029,7 +1029,7 @@ without deallocating anything (and so unbounded call-stack depth is avoided).
10291029When the call stack finishes unwinding again, code generated by the END macro
10301030notices this, and calls another routine to deallocate all the objects that
10311031may 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,
10331033with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL.
10341034*/
10351035
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments