diff --git a/internal/sse/server.go b/internal/sse/server.go index 0c55032ec..f65817fb1 100644 --- a/internal/sse/server.go +++ b/internal/sse/server.go @@ -60,6 +60,7 @@ type Server struct { type client struct { ch chan string closeCh chan struct{} + closeOnce sync.Once flusher http.Flusher w http.ResponseWriter req *http.Request @@ -122,7 +123,7 @@ func (s *Server) ListenAndServe() error { func (s *Server) Shutdown(ctx context.Context) error { s.clientsMu.Lock() for c := range s.clients { - close(c.closeCh) + c.stop() } s.clientsMu.Unlock() return s.http.Shutdown(ctx) @@ -206,12 +207,18 @@ func (s *Server) handleStream(w http.ResponseWriter, r *http.Request) { <-r.Context().Done() // Unregister client - close(c.closeCh) + c.stop() s.clientsMu.Lock() delete(s.clients, c) s.clientsMu.Unlock() } +// stop ends the client's pump. Shutdown and the client's own handler both +// stop it, in either order. +func (c *client) stop() { + c.closeOnce.Do(func() { close(c.closeCh) }) +} + func (c *client) pump() { t := time.NewTicker(c.heartbeat) defer t.Stop() diff --git a/internal/sse/server_test.go b/internal/sse/server_test.go new file mode 100644 index 000000000..be7c21e34 --- /dev/null +++ b/internal/sse/server_test.go @@ -0,0 +1,98 @@ +/* +Copyright 2026 The Cozystack Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package sse + +import ( + "context" + "errors" + "io" + "log" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" + "time" +) + +// streamWriter accepts the first okWrites writes and fails every one after. +type streamWriter struct { + mu sync.Mutex + header http.Header + okWrites int + body strings.Builder +} + +func (w *streamWriter) Header() http.Header { return w.header } +func (w *streamWriter) WriteHeader(int) {} +func (w *streamWriter) Flush() {} + +func (w *streamWriter) Write(p []byte) (int, error) { + w.mu.Lock() + defer w.mu.Unlock() + if w.okWrites == 0 { + return 0, errors.New("connection reset") + } + w.okWrites-- + return w.body.Write(p) +} + +func (w *streamWriter) String() string { + w.mu.Lock() + defer w.mu.Unlock() + return w.body.String() +} + +func (s *Server) snapshotClients() []*client { + s.clientsMu.RLock() + defer s.clientsMu.RUnlock() + var out []*client + for c := range s.clients { + out = append(out, c) + } + return out +} + +func newTestServer(heartbeat time.Duration) *Server { + return New(Options{Heartbeat: heartbeat, Logger: log.New(io.Discard, "", 0)}) +} + +// A stream still open when the server shuts down ends when its client +// disconnects, without closing the client a second time. +func TestShutdownThenDisconnect(t *testing.T) { + s := newTestServer(time.Hour) + ctx, cancel := context.WithCancel(context.Background()) + r := httptest.NewRequest(http.MethodGet, "/stream", nil).WithContext(ctx) + done := make(chan struct{}) + go func() { + s.handleStream(&streamWriter{header: http.Header{}, okWrites: 100}, r) + close(done) + }() + deadline := time.Now().Add(time.Second) + for len(s.snapshotClients()) == 0 && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + if err := s.Shutdown(context.Background()); err != nil { + t.Fatal(err) + } + cancel() + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("the stream did not end after its client disconnected") + } +}