From ef1eebd3cd16acfe6f23777d80a93df1db08d549 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 22:41:58 +0800 Subject: [PATCH] gateway: a test waits for a held request's report before its HOME goes TestUnroutedEmptyEncodedStreamIsBounded/gzip went red with TempDir's RemoveAll "directory not empty". The cause: once magpie answers a held request, giveUp writes its report from the timer's goroutine after the answer, so the report could land in the test's temp HOME while t.TempDir's cleanup was removing it. unroutedHold now has a saved channel that giveUp closes once its report is written. watchedHold registers a cleanup (holdReported) that waits for it, up to 30s, when magpie answered the hold. Run: - Broken by hand: a sleep before the report write, with a marker for a write after cleanup. Without the wait: 3 writes after cleanup; with it: 0. - mote, at a268aadc4 + this change: gofmt -l clean; GOOS=windows go vet and GOOS=linux go vet -tags nogui ./internal/gateway ok; go test -tags nogui -count=1 -timeout 45m ./internal/gateway ok (903.8s). - Rebased on 823e7c250: gofmt clean, linux vet ok, go test -count=3 -run "Unrouted|Held" ok (159s) at the pushed head. Not tried: Windows or Linux test runs of the package. --- internal/gateway/unrouted.go | 6 +++++- internal/gateway/unrouted_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/gateway/unrouted.go b/internal/gateway/unrouted.go index 2ffa7fe9a..e39e4ec06 100644 --- a/internal/gateway/unrouted.go +++ b/internal/gateway/unrouted.go @@ -216,6 +216,9 @@ type unroutedHold struct { shown *Route // under f.mu ended bool // under f.mu: answered by magpie over bool // under f.mu: its handler returned + // saved is closed once giveUp, having answered, has written its + // report: a test waits for it before its temp HOME goes + saved chan struct{} // masking counts the maskings under way for it: one held there waits // for Settings → Privacy's local model, and its error says so masking atomic.Int32 @@ -586,7 +589,7 @@ func (s *Server) watchUnrouted(w http.ResponseWriter, r *http.Request, f *inflig return w, r, func() {} } ctx, cancel := context.WithCancelCause(r.Context()) - u := &unroutedHold{s: s, f: f, w: newHeldWriter(w), cancel: cancel, agent: callerFrom(r).agent} + u := &unroutedHold{s: s, f: f, w: newHeldWriter(w), cancel: cancel, agent: callerFrom(r).agent, saved: make(chan struct{})} r = r.WithContext(context.WithValue(ctx, unroutedKey{}, u)) if r.Body != nil && r.Body != http.NoBody { r.Body = waitedBody{r.Body, u} @@ -712,6 +715,7 @@ func (u *unroutedHold) giveUp(unknown bool) { _ = http.NewResponseController(u.w.w).SetReadDeadline(time.Now()) } u.ended = true + defer close(u.saved) if u.shown == nil { u.showLocked() } diff --git a/internal/gateway/unrouted_test.go b/internal/gateway/unrouted_test.go index 4f30c8265..8b7463e54 100644 --- a/internal/gateway/unrouted_test.go +++ b/internal/gateway/unrouted_test.go @@ -52,6 +52,9 @@ func watchedHold(t *testing.T, s *Server) *unroutedHold { } s.active.mu.Unlock() if u != nil { + // a hold magpie answered writes its report after the answer, + // from its timer: the test's temp HOME stays until it has + t.Cleanup(func() { holdReported(t, u) }) return u } if time.Now().After(deadline) { @@ -61,6 +64,22 @@ func watchedHold(t *testing.T, s *Server) *unroutedHold { } } +// holdReported waits for u's report when magpie answered it. +func holdReported(t *testing.T, u *unroutedHold) { + t.Helper() + u.f.mu.Lock() + ended := u.ended + u.f.mu.Unlock() + if !ended { + return + } + select { + case <-u.saved: + case <-time.After(30 * time.Second): + t.Error("a held request's report was never written") + } +} + // unroutedReply is a reply postWhole got. type unroutedReply struct { code int