guard against nil connections on write to protect against concurrent listener removals.

This commit is contained in:
fiatjaf
2026-04-03 12:30:06 -03:00
parent 2735abe060
commit 371cecdb84
+4
View File
@@ -2,6 +2,7 @@ package khatru
import (
"context"
"fmt"
"net/http"
"sync"
@@ -31,6 +32,9 @@ type WebSocket struct {
}
func (ws *WebSocket) WriteJSON(any any) error {
if ws == nil {
return fmt.Errorf("connection doesn't exist")
}
ws.mutex.Lock()
err := ws.conn.WriteJSON(any)
ws.mutex.Unlock()