FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156371: Fix missing PyRefTracer_DESTROY events for trashcan objects by corona10 · Pull Request #156372 · python/cpython · GitHub

/ cpython Public
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
15 changes: 15 additions & 0 deletions Lib/test/test_capi/test_object.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,20 @@ def test_pyobject_dump(self):
self.assertRegex(output, r'<object at .* is freed>')


class RefTracerTest(unittest.TestCase):
@support.skip_emscripten_stack_overflow()
@support.skip_wasi_stack_overflow()
def test_destroy_traced_for_trashcan_deferred_objects(self):
depth = 200_000
with support.disable_gc():
chain = None
for _ in range(depth):
chain = [chain]
_testcapi.start_counting_list_destroys()
del chain
destroys = _testcapi.stop_counting_list_destroys()
self.assertEqual(destroys, depth)


if __name__ == "__main__":
unittest.main()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix missing ``PyRefTracer_DESTROY`` events for trashcan deferred objects.
Patch by Donghee Na.
33 changes: 33 additions & 0 deletions Modules/_testcapimodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static struct PyModuleDef _testcapimodule;
// Module state
typedef struct {
PyObject *error; // _testcapi.error object
Py_ssize_t list_destroys;
} testcapistate_t;

static testcapistate_t*
Expand Down Expand Up @@ -2418,6 +2419,36 @@ test_reftracer(PyObject *ob, PyObject *Py_UNUSED(ignored))
return NULL;
}

static int
_listdestroytracer(PyObject *obj, PyRefTracerEvent event, void *data)
{
if (event == PyRefTracer_DESTROY && PyList_CheckExact(obj)) {
_Py_atomic_add_ssize((Py_ssize_t *)data, 1);
}
return 0;
}

static PyObject *
start_counting_list_destroys(PyObject *self, PyObject *Py_UNUSED(ignored))
{
testcapistate_t *state = get_testcapi_state(self);
_Py_atomic_store_ssize(&state->list_destroys, 0);
if (PyRefTracer_SetTracer(_listdestroytracer, &state->list_destroys) != 0) {
return NULL;
}
Py_RETURN_NONE;
}

static PyObject *
stop_counting_list_destroys(PyObject *self, PyObject *Py_UNUSED(ignored))
{
if (PyRefTracer_SetTracer(NULL, NULL) != 0) {
return NULL;
}
return PyLong_FromSsize_t(
_Py_atomic_load_ssize(&get_testcapi_state(self)->list_destroys));
}

static PyObject *
function_set_warning(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
Expand Down Expand Up @@ -3022,6 +3053,8 @@ static PyMethodDef TestMethods[] = {
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
{"test_buildvalue_p", test_buildvalue_p, METH_NOARGS},
{"test_reftracer", test_reftracer, METH_NOARGS},
{"start_counting_list_destroys", start_counting_list_destroys, METH_NOARGS},
{"stop_counting_list_destroys", stop_counting_list_destroys, METH_NOARGS},
{"_test_thread_state", test_thread_state, METH_VARARGS},
{"gilstate_ensure_release", gilstate_ensure_release, METH_NOARGS},
#ifndef MS_WINDOWS
Expand Down
4 changes: 4 additions & 0 deletions Objects/object.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3224,6 +3224,10 @@ _PyTrash_thread_destroy_chain(PyThreadState *tstate)
* up distorting allocation statistics.
*/
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
#ifdef Py_TRACE_REFS
_Py_ForgetReference(op);
#endif
_PyReftracerTrack(op, PyRefTracer_DESTROY);
(*dealloc)(op);
}
}
Expand Down

Back | FazBrowse Home | New Git URL