Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-7937 fix(realtime): guard client send channel teardown (#9145)
Zheng Haoran804646324eea
diff --git a/server/internal/realtime/hub.go b/server/internal/realtime/hub.goindex 45bce51c36..807433bde0 100644--- a/server/internal/realtime/hub.go+++ b/server/internal/realtime/hub.go@@ -227,6 +227,12 @@ type Client struct {userID stringworkspaceID string+ // sendMu guards sendClosed so a late inbound-response or fanout send+ // can never write to the closed send channel. closeSend flips+ // sendClosed under sendMu before closing send, mirroring daemonws.+ sendMu sync.Mutex+ sendClosed bool+// subscriptions is guarded by hub.mu. Tracks the scopes this client is// currently in. Used to clean up rooms on disconnect.subscriptions map[scopeKey]bool@@ -364,7 +370,7 @@ func (h *Hub) removeClient(client *Client) {}}}- close(client.send)+ client.closeSend()cb := h.onLastSubscribertotal := len(h.clients)h.mu.Unlock()@@ -509,10 +515,9 @@ func (h *Hub) BroadcastToScopeDedup(scopeType, scopeID string, message []byte, eif !client.markSeen(eventID) {continue}- select {- case client.send <- message:+ if client.trySend(message) {sent++- default:+ } else {slow = append(slow, client)}}@@ -545,10 +550,9 @@ func (h *Hub) fanoutAllDedup(message []byte, excludeWorkspace, eventID string) {if !client.markSeen(eventID) {continue}- select {- case client.send <- message:+ if client.trySend(message) {sent++- default:+ } else {slow = append(slow, client)}}@@ -641,7 +645,7 @@ func (h *Hub) evictSlow(slow []*Client) {}}c.subscriptions = nil- close(c.send)+ c.closeSend()evicted++}cb := h.onLastSubscriber@@ -1031,15 +1035,41 @@ func (c *Client) handleUnsubscribe(scope, id string) {// sendJSON best-effort encodes v and pushes it to the client's send channel.// Drops the message if the channel is full (the writePump will be evicted by// the next BroadcastToScope cycle).+// trySend delivers frame to the write pump without blocking and without+// ever writing to a closed channel (safe against concurrent teardown).+// Returns false when the buffer is full or the connection is closing.+func (c *Client) trySend(frame []byte) bool {+ c.sendMu.Lock()+ defer c.sendMu.Unlock()+ if c.sendClosed {+ return false+ }+ select {+ case c.send <- frame:+ return true+ default:+ return false+ }+}++// closeSend closes the client's send channel exactly once. Safe to call+// from removeClient and evictSlow concurrently.+func (c *Client) closeSend() {+ c.sendMu.Lock()+ defer c.sendMu.Unlock()+ if c.sendClosed {+ return+ }+ c.sendClosed = true+ close(c.send)+}+func (c *Client) sendJSON(v any) {data, err := json.Marshal(v)if err != nil {return}- select {- case c.send <- data:- default:- }+ c.trySend(data)}func (c *Client) writePump() {diff --git a/server/internal/realtime/hub_send_teardown_test.go b/server/internal/realtime/hub_send_teardown_test.gonew file mode 100644index 0000000000..20def3b2e0--- /dev/null+++ b/server/internal/realtime/hub_send_teardown_test.go@@ -0,0 +1,45 @@+package realtime++import "testing"++// TestSendOnSendChannelAfterClose verifies that a late inbound reply cannot+// panic the server after a broadcast evicts a slow client and closes its send+// channel. The broadcast returns only after eviction completes, so the test+// does not depend on goroutine scheduling to reach the closed-channel case.+func TestSendOnSendChannelAfterClose(t *testing.T) {+ hub := NewHub()+ key := sk(ScopeWorkspace, "scan-ws")+ client := &Client{+ hub: hub,+ send: make(chan []byte, 1),+ userID: "scan-user",+ workspaceID: key.ID,+ subscriptions: map[scopeKey]bool{key: true},+ }+ hub.clients[client] = true+ hub.rooms[key] = map[*Client]bool{client: true}+ client.send <- []byte(`{"type":"queued"}`)++ hub.BroadcastToScope(key.Type, key.ID, []byte(`{"type":"broadcast"}`))+ if hub.clients[client] {+ t.Fatal("full send buffer did not evict client")+ }+ select {+ case _, open := <-client.send: // Drain the message queued before eviction.+ if !open {+ t.Fatal("queued message missing after eviction")+ }+ default:+ t.Fatal("queued message missing after eviction")+ }+ select {+ case _, open := <-client.send:+ if open {+ t.Fatal("evicted client's send channel remained open")+ }+ default:+ t.Fatal("evicted client's send channel remained open")+ }++ client.handleFrame([]byte(`{"type":"ping"}`))+}