From 6f2d6ed17b7a712c91f33c49f771db7a2f352af1 Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Fri, 13 Nov 2026 01:46:20 +0300 Subject: [PATCH] fix(sse): refuse streams that open while the server shuts down Shutdown stopped the clients registered at that moment and then shut the HTTP server down. A stream accepted in between registered after the stop loop, so nothing ended it, and http.Server.Shutdown waited for its handler. The kube-ovn plunger calls Shutdown without a deadline, so such a stream delayed stopping until the manager's graceful-shutdown timeout and then failed it. Shutdown now marks the server as closing under the same lock, and a stream that opens after that is answered 503 without being registered. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- internal/sse/server.go | 9 +++++++++ internal/sse/server_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/internal/sse/server.go b/internal/sse/server.go index 67e19cbc6..3c121a08a 100644 --- a/internal/sse/server.go +++ b/internal/sse/server.go @@ -51,6 +51,9 @@ type Server struct { clientsMu sync.RWMutex clients map[*client]struct{} + // closing is set by Shutdown under clientsMu, so no stream registers + // after the clients it stops. + closing bool // latest holds the most recent payload (sent to new clients on connect). latestMu sync.RWMutex @@ -122,6 +125,7 @@ func (s *Server) ListenAndServe() error { // Shutdown gracefully stops the server. func (s *Server) Shutdown(ctx context.Context) error { s.clientsMu.Lock() + s.closing = true for c := range s.clients { c.stop() } @@ -184,6 +188,11 @@ func (s *Server) handleStream(w http.ResponseWriter, r *http.Request) { // Register client s.clientsMu.Lock() + if s.closing { + s.clientsMu.Unlock() + http.Error(w, "server is shutting down", http.StatusServiceUnavailable) + return + } s.clients[c] = struct{}{} s.clientsMu.Unlock() diff --git a/internal/sse/server_test.go b/internal/sse/server_test.go index ecb0bcd54..577d19308 100644 --- a/internal/sse/server_test.go +++ b/internal/sse/server_test.go @@ -244,3 +244,32 @@ func TestStreamWaitsForPumpBeforeReturning(t *testing.T) { t.Fatal("the handler did not return once its pump finished") } } + +// A stream that opens once Shutdown has run is refused instead of being +// registered after the clients were stopped, where nothing would end it. +func TestStreamOpenedDuringShutdownIsRefused(t *testing.T) { + s := newTestServer(time.Hour) + if err := s.Shutdown(context.Background()); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + rec := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx) + done := make(chan struct{}) + go func() { + s.handleStream(rec, r) + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("a stream opened after Shutdown stayed open") + } + if rec.Code != http.StatusServiceUnavailable { + t.Errorf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable) + } + if len(s.snapshotClients()) != 0 { + t.Error("a stream opened after Shutdown was registered") + } +}