From 6df8cf53c6e66183ecbfeb995256b685788b7c9f Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Sun, 1 May 2022 13:54:36 -0700 Subject: [PATCH] Don't close channel if it's already closed --- client/main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/client/main.go b/client/main.go index f6b7b19..79504f4 100644 --- a/client/main.go +++ b/client/main.go @@ -106,6 +106,7 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err c.chs[chID] = make(chan *types.Response, 5) c.chMtx.Unlock() + channelClosed := false go func() { // Get type of channel elements chElemType := retVal.Type().Elem() @@ -121,6 +122,8 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err // Close return channel retVal.Close() + channelClosed = true + break } // Get reflect value from channel response @@ -148,12 +151,14 @@ func (c *Client) Call(rcvr, method string, arg interface{}, ret interface{}) err break } } - c.Call("lrpc", "ChannelDone", id, nil) - // Close and delete channel - c.chMtx.Lock() - close(c.chs[chID]) - delete(c.chs, chID) - c.chMtx.Unlock() + if !channelClosed { + c.Call("lrpc", "ChannelDone", id, nil) + // Close and delete channel + c.chMtx.Lock() + close(c.chs[chID]) + delete(c.chs, chID) + c.chMtx.Unlock() + } }() } else { // IF return value is not a pointer, return error