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 <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-13 12:35:06 +03:00
1 parent 40d340a3fc
commit 6f2d6ed17b
2 files changed
+38

No files matched your search

+9
View File
@@ -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()
+29
View File
@@ -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")
}
}