If an external tool sampling GC statistics does not pause the target process (and we designed get_gc_stats not to pause), it can read torn data under certain circumstances.
There are two distinct problems:
Weakly ordered platforms: The stores of ts_start and ts_stop may be reordered by the CPU/optimizer. We need to add memory barriers to mitigate this.
Large memcpy window: We are copying a sufficiently large memory region via memcpy. If two or more GC cycles occur during this copy, we can end up with inconsistent data spanning multiple GC states.
@maurycy has an idea to use a sequence counter for consistency checks — I think he should definitely give it a try.
Activity
sergey-miryanov commented on Aug 14, 2026
I don't think we should retry more than once if the GC stats read is inconsistent. Complex retry logic should be handled by the external tools.
pythongh-155811: Retry an inconsistent GC snapshot once
gh-155811: Add a seqcount to `gc_stats` to prevent torn reads (#155828)
maurycy commented on Oct 5, 2026
@sergey-miryanov All good now? :)
sergey-miryanov commented on Oct 5, 2026
Since this is a bug fix, I think it should be backported.
I don't think we'll get this into 3.15.0, so I'm perfectly fine with waiting for 3.15.1.
cc @hugovk
hugovk commented on Oct 5, 2026
Yep, this can wait for .1
https://discuss.python.org/t/python-3-15-following-tradition-lets-have-a-surprise-rc3/109313
[3.15] gh-155811: Add a seqcount to `gc_stats` to prevent torn reads (G…
Revert "[3.15] pythongh-155811: Add a seqcount to `gc_stats` to preve…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
pythongh-146527, pythongh-155811: Backport out-of-process GC statisti…
sergey-miryanov commented on Oct 11, 2026
@hugovk Backport was reverted in c49b3dc, so I reopened it. Do you mind I reopen it again? :)
[3.15] Restore deferred backports for 3.15.1, including GH-158874 (#1…
hugovk commented on Oct 11, 2026
Re-added in #158882?
sergey-miryanov commented on Oct 11, 2026
Yes, I missed that. Thanks!