From 25bb8da5e9027ab89788615a5f15dd554d7863fd Mon Sep 17 00:00:00 2001 From: dyy8888 <47207512+dyy8888@users.noreply.github.com> Date: Mon, 6 Mar 2023 09:23:43 +0800 Subject: [PATCH] fix message return before uuid write cause stuck (#203) Co-authored-by: XingQiang Bai --- conn/channel.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/conn/channel.go b/conn/channel.go index ae0621e1..8ea22341 100644 --- a/conn/channel.go +++ b/conn/channel.go @@ -453,17 +453,24 @@ 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.mu.Lock() + 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() + delete(hc.responses, rpcMsg.uuid) + hc.mu.Unlock() 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]