From 185db76646d07c734a36c48dc1c0e5b0f74cdf3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 18 Jul 2019 13:26:09 +0200 Subject: [PATCH] jsonrpc: don't hang when remote crashes --- lib/jsonrpc/websocket.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/jsonrpc/websocket.go b/lib/jsonrpc/websocket.go index 6d21be1e0..f52c0591c 100644 --- a/lib/jsonrpc/websocket.go +++ b/lib/jsonrpc/websocket.go @@ -71,6 +71,24 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r handling := map[int64]context.CancelFunc{} var handlingLk sync.Mutex + defer func() { + for id, req := range inflight { + req.ready <- clientResponse{ + Jsonrpc: "2.0", + ID: id, + Error: &respError{ + Message: "handler: websocket connection closed", + }, + } + + handlingLk.Lock() + for _, cancel := range handling { + cancel() + } + handlingLk.Unlock() + } + }() + cancelCtx := func(req frame) { if req.ID != nil { log.Warnf("%s call with ID set, won't respond", wsCancel)