Files
magpie/internal/gateway/recent_ring_test.go
T
yetone 5090f3215b gateway: a call dropped from Recent calls is let go, not kept in the ring's array (yg6888 on X: ~1 GB)
record kept the last 40 calls by re-slicing the oldest off. The dropped
call stayed in the backing array, request and response captures and
all (up to 256 KiB each), until an append outgrew the array and copied
the ring to one about twice its size. So up to ~80 calls' bodies were
kept for a page that shows 40.

keepRecent now moves the last 39 down in place, clears the tail and
appends, so the array holds exactly the calls shown. Every reader of
s.recent copies what it needs under the lock, so moving the ring in
place changes nothing they see.

Measured, before (dee5c9d75, the `magpie web` gateway, 6 agents, 4746
requests over 60 min, average body 0.86 MB): the idle live heap was
52 MB in use. 18.7 MB of it was bytes.Buffer.String: 147 captures,
about 80 calls' worth, held by the ring. That share falls to the 40
calls shown. Allocations per request don't change (BenchmarkLargeRequest
55.37 MB/op before and after).

TestRecentRingKeepsNoDroppedBodies records 120 calls and watches each
call's bodies through weak pointers. Broken by hand (the re-slice put
back), it goes red: "46 calls dropped from the ring are still alive".

Run (darwin arm64, go1.26.8, sandbox HOME, load 40 to 90 from other
sessions' suites):
- go test ./... -count=1 -timeout 45m on the series' final tree at
  ba319a758: exit 0, all 68 packages ok (gateway 1498s, agent 1633s).
- After the rebase onto b855cfee9 (two redact commits): go test
  ./internal/redact ./internal/gateway -count=1 -timeout 45m, both ok
  (gateway 917s).
- go test -race -count=3 on the Held, ReadersOfAHeldBody, RecentRing,
  RequestBody, CaptureRequest, folder, WorkPlace, WorkDir and Turn
  tests: ok, after
  TestReadersOfAHeldBodyParseItOnce's byte bound was skipped under
  -race (the third commit says why); that test and
  TestHeldFactsGoWithTheirBody then passed -count=30 with and without
  -race.
- Each commit alone: go vet ./internal/gateway and gofmt -l clean.
- Final tree: gofmt -l clean; go vet ./... for darwin, GOOS=windows and
  GOOS=linux -tags nogui, all exit 0.
Not tried: a second 60-minute soak after the series (the one before is
the "before" here; the "after" numbers are the benchmark's), the GUI
suite (nothing in it changed), Windows and Linux test runs (vet only).
2026-10-21 19:17:09 +08:00

55 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"runtime"
"strconv"
"strings"
"testing"
"unsafe"
"weak"
)
// TestRecentRingKeepsNoDroppedBodies: a call that has left the Recent-calls
// ring is no longer kept alive by it, bodies and all. Re-slicing the oldest
// off left the dropped calls in the ring's backing array, up to about as
// many again as the ring holds, each with up to 2×256 KiB of bodies.
func TestRecentRingKeepsNoDroppedBodies(t *testing.T) {
s := &Server{}
const calls = 3 * recentMost
bodies := make([]weak.Pointer[byte], calls)
for i := range calls {
body := strings.Repeat("x", 4<<10) // its own allocation, for the weak pointer
bodies[i] = weak.Make(unsafe.StringData(body))
s.record(Call{Model: "m" + strconv.Itoa(i), RequestBody: body})
}
runtime.GC()
runtime.GC()
var kept []int
for i := range calls - recentMost {
if bodies[i].Value() != nil {
kept = append(kept, i)
}
}
if len(kept) > 0 {
t.Fatalf("%d calls dropped from the ring are still alive: %v", len(kept), kept)
}
for i := calls - recentMost; i < calls; i++ {
if bodies[i].Value() == nil {
t.Fatalf("call %d, still in the ring, lost its body", i)
}
}
got := s.Recent()
if len(got) != recentMost {
t.Fatalf("ring holds %d calls, want %d", len(got), recentMost)
}
for i, c := range got {
if want := "m" + strconv.Itoa(calls-1-i); c.Model != want {
t.Fatalf("Recent()[%d] = %s, want %s (newest first)", i, c.Model, want)
}
if c.ID != uint64(calls-i) {
t.Fatalf("Recent()[%d].ID = %d, want %d", i, c.ID, calls-i)
}
}
runtime.KeepAlive(s)
}