mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-14 02:15:57 +00:00
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:
1 parent
2739984aed
commit
57f249a89d
1 file changed
+10
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user