Repository navigation
Read on through a background collection until its pause lands in a read (#357) - #360
Open
mattlorimor wants to merge 1 commit into
Open
mattlorimor wants to merge 1 commit into
mattlorimor wants to merge 1 commit into
Conversation
…ad (#357) TestAReadIsMeasuredExactlyWithABackgroundCollectionRunning's guard failed once in 9 full net10.0 runs, and in one clean-clone run on a loaded machine on both frameworks: DpswSketch's large-form read counted 4,648,328 bytes at most, with nothing to keep a background collection out, against 4,648,328 quietly. It passed run alone, and on all five CI runners. The cause is where a background collection reaches the count. It does so once, at the pause that ends its marking: there, with the runtime suspended, it voids every thread's allocation context, zeroing its pointer and limit without taking the part left unused off alloc_bytes, where a blocking collection's fix_allocation_context takes it off; and GetAllocatedBytesForCurrentThread is alloc_bytes + alloc_bytes_uoh - (alloc_limit - alloc_ptr). So a count is high by what the read's context had left, if and only if that pause comes during the read. dotnet/runtime v10.0.14 gc.cpp: gc_heap::background_mark_phase, after bgc_suspend_EE, ends calling repair_allocation_contexts (FALSE), which applies void_allocation; comutilnative.cpp GCInterface::GetAllocatedBytesForCurrentThread. v11.0.0 has the same in background.cpp and allocation.cpp. Read 2026-11-10. Neither of the guesses the plan named: in none of 2,460 probed attempts did the collection end before the read began, and in every one it had started before it. What decides it is how long marking takes against the read. - Probed with the runtime's GC events (GCStart/End, SuspendEE/RestartEE) timing each read against its collection's pauses: 1,671 of the 1,680 reads the final pause fell in came out high, by 16 to 8,192 bytes; none of the 140 that ended before it, nor the 180 started after it (a read made to wait for the pause before starting), was disturbed. - In full-suite runs, marking (start to final pause) took 2 to 30 ms against a read of about 11 to 21 ms, varying between runs with the machine. In the three runs where it took longest (one framework alone, and both at once), 12, 16 and 19 reads in 100 came out exact, and the 16 timed against the pause had all ended before it; in eight others, both frameworks at once with up to two other suites beside them, 0 to 5 in 100 did. - With small objects added to the live heap, marking lengthened in step: 0 MB 0.8 ms, 400 MB 12.7 ms, 800 MB 21.6 ms, 1.6 GB 42 ms, against reads of 9 to 20 ms; reads ending before the pause went 0/60, 4/60, 59/60, 60/60. - What made marking slow in some full runs and not others was not pinned down. It was not which CCD of this 7950X3D the suite ran on (pinned to either, it marked in 2 to 5 ms), nor sharing one CPU (2.3 ms against a 22 ms read, run alone), and two suites looping beside it did not bring it on. So the old guard, which counted one read straight after starting each of five collections, failed whenever marking outlasted the read for the length of the test, and retrying that same attempt does not help when a heap or a machine holds the condition: at 1.6 GB every one of 60 missed. The guard now reads on while a collection runs, starting another only once it has ended, until a read counts above quiet, for at most GuardReads = 50 reads. The end of a collection is told by GCKind.Background's index, noted after the blocking collections that start each one, since those wait for one still running. The loop is CountUntilDisturbed, which the test calls and three scripted tests hold: it reads on through a collection and starts another only once it has ended; with nothing disturbed it counts every read it may and returns the most; and it reaches the longest wait measured. A fourth test holds the hook itself: a collection started while another is still marking is taken for ended once it has ended and not before, and the other once it has. The guard also gets its other direction: the read counted the same way after the same blocking collections, with no background one, must come out no higher than quiet, so a count high for another reason cannot pass for a disturbance. The cap rests on reads to a disturbance, measured with the new loop: - 0 or 400 MB added: 1 read in all of 60 waits. 800 MB: 2 in all of 30. - 1.6 GB, one framework: 3 in 28 of 30, 4 in one, 10 (in two collections) in one. 1.6 GB, both frameworks at once: 3 to 12 in 10 waits (12 over two collections), and 3 to 8 in 50 more. Longest: 12; collections that ended without disturbing a read: 2 of the 92 started at 1.6 GB. - 50 is about four times 12. Counted in a region instead, where nothing can disturb it, the read took the guard to its fiftieth read and a failure in about two seconds, run alone. Red and green under the condition made certain, a probe running the guard as it was at 7e9e63d and as it is now in one process, with both frameworks at once: - 800 MB added: old failed 10 of 10 on net10.0 and 10 of 10 on net11.0; new passed 10 of 10 on each. - 1.6 GB added: old failed 5 of 5 on each; new passed 5 of 5, and 25 of 25 more on each. - Nothing added: both passed 10 of 10 on each. In ordinary full runs on this 16-core 7950X3D, loaded with other suites, the old test did not fail: marking stayed fast, and the old guard took the most of five reads, so at the machine's low miss rate all five rarely missed at once. It passed 30 full runs on each framework, loaded with two other suites. The failure is reproduced by lengthening marking, not by load alone here, which is why the probe adds to the live heap; the original report had it once in 9 net10.0 runs on another machine. Green, the fix in full suite runs with both frameworks: 10 loaded two-framework rounds with two other suites looping beside each, and 3 net10.0 runs alone, all passed the test, which on this fast machine is disturbed at its first read every time. Each framework runs 2,711 tests, 4 more than 7e9e63d's 2,707: the one test becomes itself plus three that hold the read-on loop from a script and one that holds the end-of-collection hook. Mutation through scripts/mutate.sh on this commit, scoped to TestAllocationMeasurement, both frameworks (the script reads each framework's summary). Every condition the fix adds, dropped, flipped or shifted; the counts are failing tests on net10.0 and net11.0: G1 guard counts in a region, so no read is ever disturbed killed (1, 1) G2 the background collection made blocking killed (2, 2) G3 cap of 1 killed (1, 1) G4 cap of 11, one short of the longest wait measured killed (1, 1) G5 every count without a region 8 bytes high killed (1, 1) G6 the undisturbed control counts with a collection started killed (1, 1) L1 a count equal to quiet taken for a disturbance killed (3, 3) L2 a collection started before every read, not only when ended killed (3, 3) L3 no collection started after the first killed (2, 2) L4 one read short of the cap killed (1, 1) L5 one read past the cap killed (1, 1) L6 the last count returned, not the most killed (1, 1) L7 the least count returned, not the most killed (1, 1) L8 collections not counted killed (3, 3) L9 nothing disturbed, but one over the most returned killed (1, 1) S1 the last end noted before the blocking collections killed (1, 1) S2 the end told by an index that has not changed killed (1, 1) S3 a collection never taken for ended killed (1, 1) S4 no blocking collection before the background one killed (1, 1) 19 killed, 0 survived. The brief's two named mutants are G1 and G3: G1 changes only the guard's own count, to one taken in a region where no collection can disturb it, so a disturbance never happens -- killed by the guard test itself, the one failing test on each framework, which is the vacuity guard still biting. G3 is killed by TestTheGuardReadsAsFarAsTheLongestWaitForADisturbanceMeasured, which fails by count whatever the machine, so the cap is pinned deterministically rather than by a race this fast machine happens to win. TESTING.md records the lesson beside #293's. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KK3MspnYsGe3E6AZW1Prra
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.
Fixes #357.
TestAReadIsMeasuredExactlyWithABackgroundCollectionRunning's vacuity guard failed intermittently: it needs a background collection to disturb an unprotected allocation count, and sometimes the collection didn't land during the read, so the guard fired ("the collection did not disturb the count and this shows nothing"). It got far worse once the suite runs two frameworks at once, which is why it surfaced with the .NET 11 multi-target (#358). Test-only; no library change.The cause (anchored in the runtime source)
A background collection reaches
GetAllocatedBytesForCurrentThreadonce, at the pause that ends its marking. There, with the EE suspended, the GC voids every thread's allocation context (void_allocation) without subtracting the unused tail fromalloc_bytes— where a blocking collection'sfix_allocation_contextdoes subtract it. The per-thread counter isalloc_bytes + alloc_bytes_uoh − (alloc_limit − alloc_ptr), so a read's count is high by whatever its context had left iff that end-of-marking pause falls inside the read, and exact otherwise.Sources, read 2026-11-10:
gc.cppbackground_mark_phase→repair_allocation_contexts(FALSE)→void_allocation, andcomutilnative.cppGetAllocatedBytesForCurrentThread, atv10.0.14; identical atv11.0.0(background.cpp/allocation.cpp).Neither guess in the issue held. Over 2,460 probed attempts, the collection never ended before the read began, and always started before it. What decides disturbance is marking duration vs read duration: adding small live objects lengthened marking in lockstep (0 MB → 0.8 ms, 400 → 12.7, 800 → 21.6, 1.6 GB → 42 ms) and drove reads-ending-before-the-pause from 0/60 to 60/60. Why marking is slow in some full runs and fast in others is not determined (ruled out: CCD placement, CPU sharing, sibling suites); the test and TESTING.md say so rather than guess.
The fix
The guard now reads on while a collection is running, starting the next read only once the current collection has ended (by
GCKind.Backgroundindex, after the blocking pre-collections that drain any in-flight one), until a count exceeds quiet — capped atGuardReads = 50. It still fails if nothing is ever disturbed, so it stays non-vacuous.Added the other direction: the same read after the same blocking collections with no background one must come out ≤ quiet, so a count that is high for some other reason can't masquerade as a disturbance.
The retry logic is factored into
CountUntilDisturbed, held by three scripted tests (reads-on-and-restarts; counts-every-read-and-returns-the-most when nothing is disturbed; reaches the longest wait) plus a hook test for the end-of-collection signal.The cap, from measured attempt counts
Reads-to-a-disturbance: 400 MB added → 1; 800 MB → 2; 1.6 GB, one framework → mostly 3 (one 4, one 10); 1.6 GB, both frameworks at once → 3–12, longest 12.
GuardReads = 50is ~4× that longest wait. A never-disturbed guard, counted in a no-GC region, hits 50 and fails in ~2 s run alone.Red → green
With the condition made certain (small live objects added to lengthen marking), one process, both frameworks:
On a fast 7950X3D the old test doesn't fail in ordinary loaded full runs (0 in 30 per framework) because marking stays fast and the old max-of-5 rarely missed all five; that's why reproduction needs the enlarged heap, and why it was seen only ~1-in-9 on another machine.
Mutation
scripts/mutate.sh, scoped toTestAllocationMeasurement, both frameworks: 19 mutants, 19 killed. Including the two the issue named — the guard counting inside a no-GC region so no disturbance can ever happen (killed by the guard test, so the vacuity check still bites), and a cap of 1 (killed deterministically by the longest-wait scripted test, so the cap is pinned by construction, not by a race). Also blocking vs background collection, cap 11 (one short of 12), the counter ±8, the undisturbed-control direction, every loop condition, and all four end-of-collection signal mutants. The table is in the commit message.Verification
-warnaserror: exit 0 from scratch.🤖 Generated with Claude Code
https://claude.ai/code/session_01KK3MspnYsGe3E6AZW1Prra