From 448655c288d86d602ae5364762d89765aae30fdc Mon Sep 17 00:00:00 2001 From: dborup Date: Wed, 2 Dec 2026 14:42:05 +0000 Subject: [PATCH 1/2] test(server): reproduce the hidden-filter alloc test failing on other goroutines' allocations (#1088) TestHiddenFilterPacketsNoExtraAllocs failed in the -race catch-all shard ("50 hidden: 28 allocs, no config: 24") while it passes on its own: by the time it runs, earlier tests have left 95 schema healers behind (healSchemaFlags, a 2 s re-probe on DBs never closed), and AllocsPerRun counts their allocations too, 5-11 per filter call under -race. The test now runs a background allocator that stands in for them, and bounds the no-config path absolutely (under one allocation per 500 packets; 23 at 30K). Red: the in-process figure carries the noise. Co-Authored-By: Claude Opus 5.5 --- .../hidden_identity_packets_bench_test.go | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/cmd/server/hidden_identity_packets_bench_test.go b/cmd/server/hidden_identity_packets_bench_test.go index 6ec2844f6..153b1f298 100644 --- a/cmd/server/hidden_identity_packets_bench_test.go +++ b/cmd/server/hidden_identity_packets_bench_test.go @@ -3,6 +3,7 @@ package main import ( "fmt" "testing" + "time" ) // #1023 perf proof (AGENTS.md rule 0): the hidden-identity predicate on the @@ -147,15 +148,44 @@ func BenchmarkHiddenRebuild30K(b *testing.B) { } } +// hidNoiseSink keeps hidAllocNoise's allocations on the heap. +var hidNoiseSink []byte + +// hidAllocNoise allocates in the background until the test ends. It stands +// in for the goroutines earlier tests leave running in the same binary: in +// the -race shard (#1088) 95 schema healers, each re-probing its DB every +// 2 s, allocated 5-11 times per filter call while it was measured. +func hidAllocNoise(t *testing.T) { + stop, done := make(chan struct{}), make(chan struct{}) + t.Cleanup(func() { close(stop); <-done }) + go func() { + defer close(done) + tick := time.NewTicker(time.Millisecond) + defer tick.Stop() + for { + select { + case <-stop: + return + case <-tick.C: + for i := 0; i < 1000; i++ { + hidNoiseSink = make([]byte, 16) + } + } + } + }() +} + // TestHiddenFilterPacketsNoExtraAllocs enforces the per-packet claim: the // filter over 30K packets allocates no more per call with nothing hidden, // or with 50 hidden identities, than with no hiding configured — a few -// constant allocations at most (the predicate), never one per packet. -// AllocsPerRun counts every goroutine's allocations, and a full test run -// leaves background goroutines behind, so each mode takes the least of a -// few measurements and a small constant slack is allowed. +// constant allocations at most (the predicate), never one per packet — and +// with no hiding configured less than one per 500 packets (the result slice +// grows 23 times to 30K). AllocsPerRun counts every goroutine's allocations, and +// a full test run leaves background goroutines behind, so each mode takes +// the least of a few measurements and a small constant slack is allowed. func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { const slack = 3 + hidAllocNoise(t) s := hidBenchStore(t, 30000, 2000, 50, "off") allocs := func(prefixes []string) (float64, int) { hidBenchConfigure(s, prefixes) @@ -176,6 +206,9 @@ func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { off, nOff := allocs(hidBenchModes[0].prefixes) none, nNone := allocs(hidBenchModes[1].prefixes) hid, nHid := allocs(hidBenchModes[2].prefixes) + if off > float64(nOff)/500 { + t.Errorf("no config: %v allocs for %d packets", off, nOff) + } if none > off+slack || nNone != nOff { t.Errorf("nothing hidden: %v allocs / %d packets, no config: %v / %d", none, nNone, off, nOff) } From 7d4937bbc2beb5f96d70d5e89044ad7133c8c74d Mon Sep 17 00:00:00 2001 From: dborup Date: Wed, 2 Dec 2026 15:06:37 +0000 Subject: [PATCH 2/2] fix(server): count the hidden filter's allocations in a child process (#1088) AllocsPerRun reads the process-wide malloc count. In the -race catch-all shard, goroutines left behind by earlier tests (95 schema healers re-probing unclosed DBs every 2 s) allocated 5-11 times per filter call while it was measured, so "50 hidden" could exceed "no config" by more than the slack of 3 with no change in the filter (isolated: 23 / 23 / 24 under -race on master and on 6e49b0d1). The test now re-runs itself alone in a child process, as TestNodeTxSetsRetainedHeap_534 does for its heap figure, and asserts on the figures the child prints. The product code is unchanged. Co-Authored-By: Claude Opus 5.5 --- .../hidden_identity_packets_bench_test.go | 73 ++++++++++++++----- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/cmd/server/hidden_identity_packets_bench_test.go b/cmd/server/hidden_identity_packets_bench_test.go index 153b1f298..e8c0f1bdb 100644 --- a/cmd/server/hidden_identity_packets_bench_test.go +++ b/cmd/server/hidden_identity_packets_bench_test.go @@ -2,6 +2,9 @@ package main import ( "fmt" + "os" + "os/exec" + "strings" "testing" "time" ) @@ -179,13 +182,56 @@ func hidAllocNoise(t *testing.T) { // filter over 30K packets allocates no more per call with nothing hidden, // or with 50 hidden identities, than with no hiding configured — a few // constant allocations at most (the predicate), never one per packet — and -// with no hiding configured less than one per 500 packets (the result slice -// grows 23 times to 30K). AllocsPerRun counts every goroutine's allocations, and -// a full test run leaves background goroutines behind, so each mode takes -// the least of a few measurements and a small constant slack is allowed. +// with no hiding configured less than one per 500 packets (the result +// slice's growth: 20 allocations at 30K, 23 under -race). AllocsPerRun +// counts every goroutine's allocations, and goroutines earlier tests leave +// running allocate while it counts (#1088), so the figures are taken in a +// child process that runs this test alone; each mode takes the least of a +// few measurements and a small constant slack is allowed. func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { + if os.Getenv("CORESCOPE_1088_ALLOCS_CHILD") == "1" { + off, nOff, none, nNone, hid, nHid := hidFilterAllocs(t) + fmt.Printf("HID1088 off=%g/%d none=%g/%d hid=%g/%d\n", off, nOff, none, nNone, hid, nHid) + return + } const slack = 3 hidAllocNoise(t) + cmd := exec.Command(os.Args[0], "-test.run=^TestHiddenFilterPacketsNoExtraAllocs$", "-test.count=1") + cmd.Env = append(os.Environ(), "CORESCOPE_1088_ALLOCS_CHILD=1") + out, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("child: %v\n%s", err, out) + } + line := "" + for _, l := range strings.Split(string(out), "\n") { + if strings.HasPrefix(l, "HID1088 ") { + line = l + } + } + var off, none, hid float64 + var nOff, nNone, nHid int + if _, err := fmt.Sscanf(line, "HID1088 off=%g/%d none=%g/%d hid=%g/%d", &off, &nOff, &none, &nNone, &hid, &nHid); err != nil { + t.Fatalf("no measurement in the child's output (%v):\n%s", err, out) + } + t.Log(line) + if off > float64(nOff)/500 { + t.Errorf("no config: %v allocs for %d packets", off, nOff) + } + if none > off+slack || nNone != nOff { + t.Errorf("nothing hidden: %v allocs / %d packets, no config: %v / %d", none, nNone, off, nOff) + } + if hid > off+slack { + t.Errorf("50 hidden: %v allocs, no config: %v", hid, off) + } + if nHid >= nOff { + t.Errorf("50 hidden: %d packets of %d, none hidden", nHid, nOff) + } +} + +// hidFilterAllocs is the filter's allocations per call over 30K packets, +// and the packets it returns, with no hiding configured, with nothing +// hidden and with 50 hidden identities. +func hidFilterAllocs(t *testing.T) (off float64, nOff int, none float64, nNone int, hid float64, nHid int) { s := hidBenchStore(t, 30000, 2000, 50, "off") allocs := func(prefixes []string) (float64, int) { hidBenchConfigure(s, prefixes) @@ -203,19 +249,8 @@ func TestHiddenFilterPacketsNoExtraAllocs(t *testing.T) { } return least, n } - off, nOff := allocs(hidBenchModes[0].prefixes) - none, nNone := allocs(hidBenchModes[1].prefixes) - hid, nHid := allocs(hidBenchModes[2].prefixes) - if off > float64(nOff)/500 { - t.Errorf("no config: %v allocs for %d packets", off, nOff) - } - if none > off+slack || nNone != nOff { - t.Errorf("nothing hidden: %v allocs / %d packets, no config: %v / %d", none, nNone, off, nOff) - } - if hid > off+slack { - t.Errorf("50 hidden: %v allocs, no config: %v", hid, off) - } - if nHid >= nOff { - t.Errorf("50 hidden: %d packets of %d, none hidden", nHid, nOff) - } + off, nOff = allocs(hidBenchModes[0].prefixes) + none, nNone = allocs(hidBenchModes[1].prefixes) + hid, nHid = allocs(hidBenchModes[2].prefixes) + return }