gateway: its end waits for the MCP hub's pruner, which reads MCPHub
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 at3dbbb173e(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), at90efddabaplus 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.
This commit is contained in:
@@ -888,7 +888,11 @@ func (s *Server) ListenAndServe(ctx context.Context) error {
|
||||
go filememo.Watch(freshCtx, pluginrt.Path(), settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())
|
||||
go pluginrt.KeepFresh(freshCtx, filememo.OthersEvery, settings.Path(), provider.Path(), plugin.ListPath(), plugin.ToolsPath(), access.Path())
|
||||
// and the MCP hub's servers that the Library no longer gives are ended
|
||||
go keepHubPruned(freshCtx)
|
||||
// It reads MCPHub, which a test swaps, so the gateway's end waits for
|
||||
// it: once ListenAndServe has returned, nothing it started still looks.
|
||||
pruned := make(chan struct{})
|
||||
go func() { defer close(pruned); keepHubPruned(freshCtx) }()
|
||||
defer func() { stopFresh(); <-pruned }()
|
||||
srv := &http.Server{Handler: s.closesWhenHandedOff(s.Front()), ReadHeaderTimeout: 30 * time.Second, IdleTimeout: 5 * time.Minute, ConnState: s.unread.note}
|
||||
// the magpie serving the gateway, and only it, keeps the saved accounts
|
||||
// signed in, so two never refresh one sign-in at once
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// heldHub's Prune holds until released, and says while it runs.
|
||||
type heldHub struct {
|
||||
fakeHub
|
||||
entered chan struct{}
|
||||
release chan struct{}
|
||||
running atomic.Bool
|
||||
}
|
||||
|
||||
func (h *heldHub) Prune() {
|
||||
if !h.running.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case h.entered <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
<-h.release
|
||||
h.running.Store(false)
|
||||
}
|
||||
|
||||
// The hub's pruner reads MCPHub, which tests swap: once ListenAndServe has
|
||||
// returned, the pruner it started has ended, so a test that swaps MCPHub
|
||||
// after a gateway's end races nothing (TestInfoSaysMCPHub, -race on CI at
|
||||
// 3dbbb173e). A Prune held mid-call keeps the gateway's end waiting.
|
||||
func TestGatewayEndEndsHubPruner(t *testing.T) {
|
||||
fresh(t)
|
||||
t.Setenv("MAGPIE_ADDR", freeAddr(t))
|
||||
was := hubPruneEvery
|
||||
hubPruneEvery = time.Millisecond
|
||||
t.Cleanup(func() { hubPruneEvery = was })
|
||||
h := &heldHub{entered: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
old := MCPHub
|
||||
MCPHub = h
|
||||
t.Cleanup(func() { MCPHub = old })
|
||||
stop, done := listening(t, New())
|
||||
select {
|
||||
case <-h.entered:
|
||||
case <-time.After(10 * time.Second):
|
||||
stop()
|
||||
<-done
|
||||
t.Fatal("the hub was never pruned")
|
||||
}
|
||||
stop()
|
||||
select {
|
||||
case <-done:
|
||||
close(h.release)
|
||||
t.Fatal("the gateway ended while its pruner was still in Prune")
|
||||
case <-time.After(time.Second):
|
||||
}
|
||||
close(h.release)
|
||||
<-done
|
||||
if h.running.Load() {
|
||||
t.Error("the pruner still ran after the gateway ended")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user