Repository navigation
test(smart_buffer): deflake TestRAMChunk_ReleaseReusesBlocks - #1056
Conversation
The test asserted that a block released to blockPools is handed back to the next chunk, but it ran in parallel with the other RAMChunk tests, which share the same pools and can take the released blocks first. sync.Pool may also drop any item it is given, and under the race detector it drops one Put in four on purpose. The test failed 30 times in 1000 runs with -race (and occasionally without it). Run it serially and allow up to ten release/reuse rounds. The assertion is unchanged: at least one released block must come back from the pool. It still fails 5/5 when Release stops returning blocks or getBlock stops taking them, and passed 1000/1000 with -race after the change.
rita7lopes
left a comment
There was a problem hiding this comment.
Thanks, the diagnosis is right, and this fixes a real flake. I reproduced it, and it also happens without -race, so it can hit CI today:
TestRAMChunk_*, 1000 runs |
master |
this branch |
|---|---|---|
-race |
39 failures | 0 |
| no race | 12 failures | 0 |
One change before merging: the retry loop stops the test catching a bug it catches today. I injected a few bugs into ram_chunk.go and ran the test 200 times against each:
| Injected bug | master catches |
this branch catches |
|---|---|---|
Release() never returns blocks |
200/200 | 200/200 |
getBlock() never reuses blocks |
200/200 | 200/200 |
Release() doesn't reset size |
200/200 | 200/200 |
putBlock uses the wrong size class ((blockClass(cap(b))+1)%blockClasses) |
200/200 | 0/200 |
With the wrong size class, round 1 fails correctly, but each retry starts from the blocks the previous round already mixed up, so a pointer match turns up by chance:
round 1 first=[8 16 32 64]KB r2=[8 8 16 16 32]KB match@-1
round 2 first=[8 8 8 16 16 16]KB r2=[8 8 8 8 16 16]KB match@2
The blocks keep shrinking towards all 8KB, which defeats the block ladder, and no other test in the package caught it in 20 full-package runs.
Could you check block sizes inside the retry loop, before looking for a reused block?
for i, block := range r2.blocks {
if want := min(initialBlockSize<<i, maxBlockSize); cap(block) != want {
t.Fatalf("block %d has capacity %d, want %d", i, cap(block), want)
}
}With this added, the wrong-size-class bug was caught 200/200 times. Without any injected bug, the RAMChunk tests had 0 failures in 1000 runs, both with and without -race.
Two smaller things:
- Title: the test is now very unlikely to fail by chance rather than deterministic, so
deflakewould be more accurate thanmake ... deterministic. - The other
-racefailure: the description says one of the two failinggo test -race ./...runs didn't keep its output. I only tested this package, so that one may be a separate flake. It's not a blocker here, but it's worth tracking down before-racebecomes a required check under #918.
A retry starts from the blocks the previous round returned. If putBlock put blocks in the wrong size class, a later round could find a reused block by chance and pass. Check that every block has its ladder size before looking for a reused one.
|
Thanks @rita7lopes. I added your size check inside the retry loop and renamed the PR to "deflake". Re-ran your injected bugs, 200 runs each: the wrong size class is now caught 200/200 (it was 0/200), the other three are still 200/200, and with no bug there were 0 failures. The |
|
Follow-up on the other |
rita7lopes
left a comment
There was a problem hiding this comment.
Thanks @Guess-Who-sec, this looks good. I re-ran everything on the new head merged with current master:
- Flakiness: 0 failures in 1000 runs of the RAMChunk tests and 200 runs of the whole package, with and without -race.
- Injected bugs: the wrong size class is now caught 200/200, and the other three are still 200/200.
Thanks also for checking the other -race failure and for keeping output from now on.
What
TestRAMChunk_ReleaseReusesBlocks(reader/utils/smart_buffer) is flaky. Its assertion is unchanged: at least one block released toblockPoolsmust be handed back to the next chunk. Three things change around it:Test-only change. No production code is touched.
Why
The test failed intermittently for two reasons:
t.Parallel()next to the otherRAMChunktests. All of them share the package-levelblockPools, so a parallel test can take the released blocks first.sync.Poolmay drop items. Any item put into async.Poolcan be dropped, and under the race detectorPutdrops one item in four on purpose (sync/pool.go, therace.Enabledbranch ofPut).It came up while preparing M1 (#918), which adds
-raceto the unit-test lane. Locally,go test -race ./...failed in 2 of 7 runs. One failure's output was not kept; the other was this test alone:Verification
RAMChunktests)master,-race, 1000 runs-race, 1000 runsThe test still catches the bugs the
masterversion catches (all four, per the review). Each bug was injected intoram_chunk.goon its own, and the test was run 200 times as separate processes:Release()never returns blocksgetBlock()never reuses blocksRelease()doesn't reset sizeputBlockuses the wrong size classAlso ran
gofmt -l .(clean),go build,go veton the package,go test ./..., andgo test -raceon the package.