From 760129610e5b54d548247490899a8595cea2d5b8 Mon Sep 17 00:00:00 2001 From: zhangxiaoyu Date: Mon, 31 Oct 2022 17:15:51 +0800 Subject: [PATCH] kubelet fix websocket reference nul pointer Signed-off-by: zhangxiaoyu --- .../cri/streaming/remotecommand/proxy.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/kubelet/cri/streaming/remotecommand/proxy.go b/pkg/kubelet/cri/streaming/remotecommand/proxy.go index 76222d90..f21629af 100644 --- a/pkg/kubelet/cri/streaming/remotecommand/proxy.go +++ b/pkg/kubelet/cri/streaming/remotecommand/proxy.go @@ -169,14 +169,23 @@ func connectBackend(addr, subprotocol string, r *http.Request) (*websocket.Conn, websocket.DefaultDialer.ReadBufferSize = 128 * 1024 websocket.DefaultDialer.WriteBufferSize = 128 * 1024 ws, resp, err := websocket.DefaultDialer.Dial(addr, h) - if err != nil { + if err == nil { + return ws, nil + } + msg := fmt.Errorf("dial failed: %v, response Body is nil", err) + if resp != nil && resp.Body != nil { + defer func() { + //websocket buffer size maybe not enough and cause panic + if e := recover(); e != nil { + msg = fmt.Errorf("dial failed: %v, response panic %v", err, e) + } + resp.Body.Close() + }() var body bytes.Buffer body.ReadFrom(resp.Body) - defer resp.Body.Close() - msg := fmt.Errorf("dial failed: %v, response is: %v", err, body.String()) - return nil, msg + msg = fmt.Errorf("dial failed: %v, response is: %v", err, body.String()) } - return ws, nil + return nil, msg } type rwc struct { -- 2.25.1