Skip to content

Commit a0c7278

Browse files
committed
Fix crash in SSE Handler
2026/02/15 22:41:29 API.SSE Write timeout panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x674385] goroutine 1588563 [running]: bufio.(*Writer).Flush(0x1f4f9510ef80) /usr/local/go/src/bufio/bufio.go:642 +0x45 net/http.(*response).FlushError(0x1f4f96cc61e0) /usr/local/go/src/net/http/server.go:1728 +0x33 net/http.(*response).Flush(0xad3720?) /usr/local/go/src/net/http/server.go:1721 +0x13 github.com/glimesh/broadcast-box/internal/server/handlers.sseHandler.func1() /broadcast-box/internal/server/handlers/sse.go:70 +0xc8 created by github.com/glimesh/broadcast-box/internal/server/handlers.sseHandler in goroutine 1519769 /broadcast-box/internal/server/handlers/sse.go:67 +0x6e5
1 parent cd8fdb0 commit a0c7278

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

  • internal/server/handlers

internal/server/handlers/sse.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package handlers
22

33
import (
4-
"context"
4+
"errors"
55
"fmt"
66
"log"
77
"net/http"
@@ -29,8 +29,10 @@ func sseHandler(responseWriter http.ResponseWriter, request *http.Request) {
2929
sessionId := values[len(values)-1]
3030

3131
debugSseMessages := strings.EqualFold(os.Getenv(environment.DEBUG_PRINT_SSE_MESSAGES), "true")
32+
writeTimeout := 500 * time.Millisecond
3233

3334
ctx := request.Context()
35+
responseController := http.NewResponseController(responseWriter)
3436

3537
// Setup WHEP/WHIP session for SSE feed
3638
sseChannel := getWhipSessionChannel(sessionId)
@@ -60,28 +62,27 @@ func sseHandler(responseWriter http.ResponseWriter, request *http.Request) {
6062
return
6163
}
6264

63-
// Write with timeout
64-
writeCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
65-
done := make(chan error, 1)
65+
if err := responseController.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil && !errors.Is(err, http.ErrNotSupported) {
66+
log.Println("API.SSE SetWriteDeadline error:", err)
67+
return
68+
}
6669

67-
go func() {
68-
_, err := fmt.Fprintf(responseWriter, "%s\n", msg)
69-
if err == nil {
70-
flusher.Flush()
71-
}
72-
done <- err
73-
}()
70+
_, err := fmt.Fprintf(responseWriter, "%s\n", msg)
71+
if err == nil {
72+
flusher.Flush()
73+
}
74+
75+
if deadlineErr := responseController.SetWriteDeadline(time.Time{}); deadlineErr != nil && !errors.Is(deadlineErr, http.ErrNotSupported) {
76+
log.Println("API.SSE ClearWriteDeadline error:", deadlineErr)
77+
return
78+
}
7479

75-
select {
76-
case err := <-done:
77-
cancel()
78-
if err != nil {
80+
if err != nil {
81+
if errors.Is(err, os.ErrDeadlineExceeded) {
82+
log.Println("API.SSE Write timeout")
83+
} else {
7984
log.Println("API.SSE Write error:", err)
80-
return
8185
}
82-
case <-writeCtx.Done():
83-
cancel()
84-
log.Println("API.SSE Write timeout")
8586
return
8687
}
8788
}

0 commit comments

Comments
 (0)