Skip to content

test(smart_buffer): deflake TestRAMChunk_ReleaseReusesBlocks - #1056

Merged
rita7lopes merged 2 commits into
metrico:masterfrom
Guess-Who-sec:test/smart-buffer-pool-flake
Nov 9, 2026
Merged

rita7lopes merged 2 commits into
metrico:masterfrom
Guess-Who-sec:test/smart-buffer-pool-flake

Conversation

@Guess-Who-sec

@Guess-Who-sec Guess-Who-sec commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

What

TestRAMChunk_ReleaseReusesBlocks (reader/utils/smart_buffer) is flaky. Its assertion is unchanged: at least one block released to blockPools must be handed back to the next chunk. Three things change around it:

  • the test no longer runs in parallel
  • it allows up to ten release/reuse rounds before failing
  • it checks block sizes in every round, so a retry can't pass by chance on blocks a previous round mixed up

Test-only change. No production code is touched.

Why

The test failed intermittently for two reasons:

  1. Shared pools. It ran with t.Parallel() next to the other RAMChunk tests. All of them share the package-level blockPools, so a parallel test can take the released blocks first.
  2. sync.Pool may drop items. Any item put into a sync.Pool can be dropped, and under the race detector Put drops one item in four on purpose (sync/pool.go, the race.Enabled branch of Put).

It came up while preparing M1 (#918), which adds -race to 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:

--- FAIL: TestRAMChunk_ReleaseReusesBlocks
    ram_chunk_test.go:69: no block was taken from the pool

Verification

Run (the four RAMChunk tests) Failures
master, -race, 1000 runs 30
this branch, -race, 1000 runs 0
this branch, no race, 1000 runs 0

The test still catches the bugs the master version catches (all four, per the review). Each bug was injected into ram_chunk.go on its own, and the test was run 200 times as separate processes:

Injected bug Before the block-size check This branch
none (no bug) 0/200 0/200
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 0/200 200/200

Also ran gofmt -l . (clean), go build, go vet on the package, go test ./..., and go test -race on the package.

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 rita7lopes left a comment

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.

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 deflake would be more accurate than make ... deterministic.
  • The other -race failure: the description says one of the two failing go 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 -race becomes 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.
@Guess-Who-sec Guess-Who-sec changed the title test(smart_buffer): make TestRAMChunk_ReleaseReusesBlocks deterministic test(smart_buffer): deflake TestRAMChunk_ReleaseReusesBlocks Nov 4, 2026
@Guess-Who-sec

Copy link
Copy Markdown
Contributor Author

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 RAMChunk tests had 0 failures in 1000 runs both with and without -race. I'll track down the other -race failure separately before -race becomes required under #918.

@Guess-Who-sec

Copy link
Copy Markdown
Contributor Author

Follow-up on the other -race failure: I couldn't reproduce it. With this PR's fix applied, the full go test -race ./... passed 80 out of 80 times: 20 on master and 20 on the M1 branch (#1059) on an idle machine, then 20 each with two runs in parallel for load. The lost failure was most likely this same test (about 4% of runs before the fix), but I can't confirm that since its output wasn't kept. I'll keep all output from now on, so if -race fails again we'll have the details.

@rita7lopes rita7lopes left a comment

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.

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.

@rita7lopes
rita7lopes merged commit c280959 into metrico:master Nov 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants