keepHubPruned ran on its own goroutine with nothing waiting for it, so
ListenAndServe could return while the pruner was still between ticks or
in Prune. It reads MCPHub, which tests swap with withHub, so a test that
waited for one gateway's end and then swapped the hub raced the old
gateway's pruner: TestInfoSaysMCPHub, ubuntu Race on CI at 3dbbb173e
(write at hub_mcp_test.go:76, previous read at hub_mcp.go:910 from a
ListenAndServe started at handover_test.go:28). The gateway's end now
cancels the pruner's context and waits for it to return, so once
ListenAndServe returns nothing it started still looks at MCPHub. In
the app this adds the wait for a Prune already running at shutdown,
which ends the servers the library no longer gives (the same b.stop
StopHub does).
TestGatewayEndEndsHubPruner holds a Prune mid-call and checks the
gateway's end waits for it.
Run on mica (macOS, Go 1.26.8), at 90efddaba plus this change:
- go test -race -count=3 -run 'TestGatewayEndEndsHubPruner|
TestInfoSaysMCPHub|TestHandedOver|TestEnding|TestListening'
./internal/gateway: ok.
- Broken by hand: with origin/main's gateway.go the new test fails 2 of
2 ("the gateway ended while its pruner was still in Prune") with a
DATA RACE report.
- GOOS=linux and GOOS=windows go vet ./internal/gateway: ok.