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 ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"sync" "sync"
@@ -31,6 +32,9 @@ type WebSocket struct {
} }
func (ws *WebSocket) WriteJSON(any any) error { func (ws *WebSocket) WriteJSON(any any) error {
if ws == nil {
return fmt.Errorf("connection doesn't exist")
}
ws.mutex.Lock() ws.mutex.Lock()
err := ws.conn.WriteJSON(any) err := ws.conn.WriteJSON(any)
ws.mutex.Unlock() ws.mutex.Unlock()