diff --git a/seaweed-block/core/host/volume/status_server.go b/seaweed-block/core/host/volume/status_server.go index 713bc55d9f..de3fbe7c37 100644 --- a/seaweed-block/core/host/volume/status_server.go +++ b/seaweed-block/core/host/volume/status_server.go @@ -391,7 +391,8 @@ func (s *StatusServer) Start(addr string) (string, error) { } s.srv = &http.Server{Handler: mux, ReadHeaderTimeout: 5 * time.Second} - go func() { _ = s.srv.Serve(ln) }() + srv := s.srv + go func() { _ = srv.Serve(ln) }() return s.addr, nil } diff --git a/seaweed-block/core/host/volume/status_server_shutdown_test.go b/seaweed-block/core/host/volume/status_server_shutdown_test.go new file mode 100644 index 0000000000..5de9082f07 --- /dev/null +++ b/seaweed-block/core/host/volume/status_server_shutdown_test.go @@ -0,0 +1,25 @@ +package volume + +import ( + "context" + "runtime" + "testing" +) + +// Keep Serve runnable until after Close: starting a listener and immediately +// shutting it down must be safe even if the Serve goroutine has not run yet. +func TestStatusServerImmediateClose(t *testing.T) { + previous := runtime.GOMAXPROCS(1) + defer runtime.GOMAXPROCS(previous) + s := NewStatusServer(NewAdapterProjectionView(stubProjector{}, "v1", "r1", nil)) + if _, err := s.Start("127.0.0.1:0"); err != nil { + t.Fatal(err) + } + if err := s.Close(context.Background()); err != nil { + t.Fatal(err) + } + runtime.Gosched() // let the delayed Serve execute before the test returns + if err := s.Close(context.Background()); err != nil { + t.Fatalf("repeated close: %v", err) + } +}