From 396982af943b00b8e384b4fdd0de3f4ec0fb92c7 Mon Sep 17 00:00:00 2001 From: Ido Kanner Date: Sun, 3 Nov 2024 09:46:18 +0200 Subject: [PATCH] trying to fix race condition when channel is closed and there is an attempt to read channel --- connection.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/connection.go b/connection.go index 75cf210..6d280bb 100644 --- a/connection.go +++ b/connection.go @@ -154,14 +154,14 @@ func (c *Conn) SendCommand(ctx context.Context, cmd command.Command) (*RawRespon c.responseChanMutex.RLock() defer c.responseChanMutex.RUnlock() select { - case response := <-c.responseChannels[TypeReply]: - if response == nil { + case response, ok := <-c.responseChannels[TypeReply]: + if !ok || response == nil { // We only get nil here if the channel is closed return nil, errors.New("connection closed") } return response, nil - case response := <-c.responseChannels[TypeAPIResponse]: - if response == nil { + case response, ok := <-c.responseChannels[TypeAPIResponse]: + if !ok || response == nil { // We only get nil here if the channel is closed return nil, errors.New("connection closed") }