fix(sse): close a client's channel once when the server shuts down

Shutdown closed every open client's channel, and the client's handler
closed the same channel again when its request ended, so any stream
still open at shutdown panicked with a close of a closed channel.
Stopping a client is now idempotent.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-13 12:34:58 +03:00
1 parent f99e511eea
commit 20ce6c691e
2 files changed
+107 -2

No files matched your search

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