From 504bec38d24f16d55ded971f03e36648394a2214 Mon Sep 17 00:00:00 2001 From: dyy8888 <2497213126@qq.com> Date: Wed, 1 Mar 2023 10:14:48 +0800 Subject: [PATCH 1/3] fix bug --- conn/channel.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/conn/channel.go b/conn/channel.go index ae0621e1..560fa6c3 100644 --- a/conn/channel.go +++ b/conn/channel.go @@ -453,17 +453,20 @@ func (hc *channelSession) doRPCRequest(ctx context.Context, msg interface{}) (io return nil, err } msgBytes := rpcMsg.Encode() + + response := &channelResponse{Message: nil, Notify: make(chan interface{})} + hc.mu.Lock() + hc.responses[rpcMsg.uuid] = response + hc.mu.Unlock() if hc.c == nil { + hc.responses[rpcMsg.uuid] = nil return nil, errors.New("connection unavailable") } _, err = hc.c.Write(msgBytes) if err != nil { + hc.responses[rpcMsg.uuid] = nil return nil, err } - response := &channelResponse{Message: nil, Notify: make(chan interface{})} - hc.mu.Lock() - hc.responses[rpcMsg.uuid] = response - hc.mu.Unlock() <-response.Notify hc.mu.Lock() response = hc.responses[rpcMsg.uuid] From 2b4eda1e5a264fabae2d2d7e02388d948d6f7282 Mon Sep 17 00:00:00 2001 From: dyy8888 <2497213126@qq.com> Date: Wed, 1 Mar 2023 10:40:25 +0800 Subject: [PATCH 2/3] add mutex --- conn/channel.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/conn/channel.go b/conn/channel.go index 560fa6c3..c0cd66c0 100644 --- a/conn/channel.go +++ b/conn/channel.go @@ -459,12 +459,16 @@ func (hc *channelSession) doRPCRequest(ctx context.Context, msg interface{}) (io hc.responses[rpcMsg.uuid] = response hc.mu.Unlock() if hc.c == nil { + hc.mu.Lock() hc.responses[rpcMsg.uuid] = nil + hc.mu.Unlock() return nil, errors.New("connection unavailable") } _, err = hc.c.Write(msgBytes) if err != nil { + hc.mu.Lock() hc.responses[rpcMsg.uuid] = nil + hc.mu.Unlock() return nil, err } <-response.Notify From 0e52de0a2ddd09c5fc260fad942066cfd7bec612 Mon Sep 17 00:00:00 2001 From: XingQiang Bai Date: Mon, 6 Mar 2023 09:18:18 +0800 Subject: [PATCH 3/3] Update channel.go --- conn/channel.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conn/channel.go b/conn/channel.go index c0cd66c0..8ea22341 100644 --- a/conn/channel.go +++ b/conn/channel.go @@ -460,14 +460,14 @@ func (hc *channelSession) doRPCRequest(ctx context.Context, msg interface{}) (io hc.mu.Unlock() if hc.c == nil { hc.mu.Lock() - hc.responses[rpcMsg.uuid] = nil + delete(hc.responses, rpcMsg.uuid) hc.mu.Unlock() return nil, errors.New("connection unavailable") } _, err = hc.c.Write(msgBytes) if err != nil { hc.mu.Lock() - hc.responses[rpcMsg.uuid] = nil + delete(hc.responses, rpcMsg.uuid) hc.mu.Unlock() return nil, err }