gh-155811: Add a seqcount to gc_stats to prevent torn reads - #155828
Open
maurycy wants to merge 1 commit into
Open
gh-155811: Add a seqcount to gc_stats to prevent torn reads#155828maurycy wants to merge 1 commit into
gc_stats to prevent torn reads#155828maurycy wants to merge 1 commit into
Conversation
Contributor
Author
maurycy
commented
Aug 15, 2026
| if (_Py_RemoteDebug_ReadRemoteMemory(&offsets->handle, | ||
| sequence_address, | ||
| sizeof(before), &before) < 0) { | ||
| set_exception_cause(offsets, PyExc_RuntimeError, |
Contributor
Author
There was a problem hiding this comment.
@sergey-miryanov Is it the best exception for gcmon?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See #155811 for the context.
tl;dr
get_gc_statsreads the stats without pausing the target (by design), so it can return torn data.The PR adds a seqcount to
gc_statsatomically increased by the writer around the stats update on every collection, where odd means it's in progress.gcmon is the main consumer.
There's no retry, as per #155811 (comment). It's not needed.
gc.collect()does not happen that often:For:
I'm not sure what's optimal non-hacky test. We haven't had one in #152448. To be honest, it's implicitly tested by the current happy-path tests in
test_gc_stats.