fix(gateway): prevent panic on send to closed WebSocket client channel

Agent run goroutines can outlive the WebSocket client (e.g. after
reconnect). When the goroutine finishes and calls SendResponse/SendEvent,
the closed send channel causes a panic that crashes the server.

Add defer/recover to both methods to gracefully drop messages for
disconnected clients instead of panicking.
This commit is contained in:
viettranx committed 2026-03-23 12:11:47 +07:00
1 parent 2739984aed
commit 57f249a89d
1 file changed
+10
+10
View File
@@ -160,6 +160,11 @@ func (c *Client) SendResponse(resp *protocol.ResponseFrame) {
slog.Error("marshal response failed", "error", err)
return
}
defer func() {
if r := recover(); r != nil {
slog.Debug("client gone, dropping response", "client", c.id)
}
}()
select {
case c.send <- data:
default:
@@ -174,6 +179,11 @@ func (c *Client) SendEvent(event protocol.EventFrame) {
slog.Error("marshal event failed", "error", err)
return
}
defer func() {
if r := recover(); r != nil {
slog.Debug("client gone, dropping event", "client", c.id)
}
}()
select {
case c.send <- data:
default: