Skip to content

Commit 215df68

Browse files
committed
Address review comments
1 parent 9a61914 commit 215df68

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

Include/cpython/object.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,30 +462,34 @@ passed as second argument to Py_TRASHCAN_BEGIN().
462462
PyAPI_FUNC(int) _PyTrash_begin(PyThreadState *tstate, PyObject *op);
463463
PyAPI_FUNC(void) _PyTrash_end(PyThreadState *tstate);
464464

465-
PyAPI_FUNC(void) PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op);
466-
PyAPI_FUNC(void) PyTrash_thread_destroy_chain(PyThreadState *tstate);
465+
PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op);
466+
PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(PyThreadState *tstate);
467467

468468

469469
/* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */
470470

471+
/* To avoid raising recursion errors during dealloc trigger trashcan before we reach
472+
* recursion limit. To avoid trashing, we don't attempt to empty the trashcan until
473+
* we have headroom above the trigger limit */
474+
#define Py_TRASHCAN_HEADROOM 50
475+
471476
#define Py_TRASHCAN_BEGIN(op, dealloc) \
472477
do { \
473478
PyThreadState *tstate = PyThreadState_Get(); \
474-
if (tstate->c_recursion_remaining <= 50 && Py_TYPE(op)->tp_dealloc == (destructor)dealloc) { \
475-
PyTrash_thread_deposit_object(tstate, (PyObject *)op); \
479+
if (tstate->c_recursion_remaining <= Py_TRASHCAN_HEADROOM && Py_TYPE(op)->tp_dealloc == (destructor)dealloc) { \
480+
_PyTrash_thread_deposit_object(tstate, (PyObject *)op); \
476481
break; \
477482
} \
478483
tstate->c_recursion_remaining--;
479484
/* The body of the deallocator is here. */
480485
#define Py_TRASHCAN_END \
481486
tstate->c_recursion_remaining++; \
482-
if (tstate->delete_later && tstate->c_recursion_remaining > 100) { \
483-
PyTrash_thread_destroy_chain(tstate); \
487+
if (tstate->delete_later && tstate->c_recursion_remaining > (Py_TRASHCAN_HEADROOM*2)) { \
488+
_PyTrash_thread_destroy_chain(tstate); \
484489
} \
485490
} while (0);
486491

487492

488-
489493
PyAPI_FUNC(void *) PyObject_GetItemData(PyObject *obj);
490494

491495
PyAPI_FUNC(int) PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg);

Objects/object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,7 @@ Py_ReprLeave(PyObject *obj)
26932693
* object, with refcount 0. Py_DECREF must already have been called on it.
26942694
*/
26952695
void
2696-
PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op)
2696+
_PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op)
26972697
{
26982698
_PyObject_ASSERT(op, _PyObject_IS_GC(op));
26992699
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
@@ -2710,7 +2710,7 @@ PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op)
27102710
/* Deallocate all the objects in the gcstate->trash_delete_later list.
27112711
* Called when the call-stack unwinds again. */
27122712
void
2713-
PyTrash_thread_destroy_chain(PyThreadState *tstate)
2713+
_PyTrash_thread_destroy_chain(PyThreadState *tstate)
27142714
{
27152715
/* We need to increase c_recursion_remaining here, otherwise,
27162716
_PyTrash_thread_destroy_chain will be called recursively

0 commit comments

Comments
 (0)