Skip to content
Open
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
5 changes: 5 additions & 0 deletions Lib/test/test_structseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,10 @@ def test_replace_gc_tracked(self):

self.assertTrue(gc.is_tracked(replaced_struct))

def test_gc_tracked(self):
# PyStructSequence objects created via C API or Python should be GC-tracked
self.assertTrue(gc.is_tracked(time.gmtime()))
self.assertTrue(gc.is_tracked(os.stat(__file__)))

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix GC tracking for :class:`!PyStructSequence` instances created via :c:func:`PyStructSequence_New`. Patch by Shamil Abdulaev.
3 changes: 1 addition & 2 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ PyStructSequence_New(PyTypeObject *type)
for (i = 0; i < size; i++)
obj->ob_item[i] = NULL;

_PyObject_GC_TRACK(obj);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks safe, but we usually try to avoid exposing partially constructed objects to the GC. I'm not sure I get why this change fixes your repro. Could you explain a bit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Why it fixes the repro: time.gmtime() is not tracked by the GC. Because of that, the GC cannot see the cycle type(t) -> t -> type(t), so the entire heap type leaks at shutdown. Tracking t lets GC traverse t -> Py_TYPE(t) and collect the cycle.

  • Partially constructed object: All items are pre-initialized to NULL. Both structseq_traverse (Py_VISIT) and structseq_dealloc (Py_XDECREF) safely ignore NULL. This follows PyTuple_New(), which also tracks the object with NULL items since C API callers have no separate "finish" step.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this change affects all call sites where PyStructSequence_New is called without a subsequent call to _PyObject_GC_TRACK. But at first glance, many of them don't seem to need tracking for PyStructSequence_New instances.

@vstinner Do we need frozen version of PyStructSequence?

return (PyObject*)obj;
}

Expand Down Expand Up @@ -265,7 +266,6 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict)
}
}

_PyObject_GC_TRACK(res);
return (PyObject*) res;
}

Expand Down Expand Up @@ -449,7 +449,6 @@ structseq_replace(PyObject *op, PyObject *args, PyObject *kwargs)
}
}

_PyObject_GC_TRACK(result);
return (PyObject *)result;

error:
Expand Down
Loading