From b8110aaf8df1a0cfa36e25e2764b20d414b88d63 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 15 Sep 2023 18:30:41 -0500 Subject: [PATCH] pool: Prevent potential read goroutine leak. This ensures the send to the read channel for a client respects the context since it could otherwise end up hanging forever when sending to the channel that no longer has a goroutine reading from it thereby leaking the goroutine. It also includes the read goroutine in the overall waitgroup for the client to help ensure it shuts down as intended. --- pool/client.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/pool/client.go b/pool/client.go index b90c91f2..de2e756a 100644 --- a/pool/client.go +++ b/pool/client.go @@ -738,7 +738,12 @@ func (c *Client) read() { c.cancel() return } - c.readCh <- readPayload{msg, reqType} + select { + case c.readCh <- readPayload{msg, reqType}: + case <-c.ctx.Done(): + c.cancel() + return + } } } @@ -1283,10 +1288,12 @@ func (c *Client) send() { // run handles the process lifecycles of the pool client. func (c *Client) run() { - go c.read() - var wg sync.WaitGroup - wg.Add(4) + wg.Add(5) + go func() { + c.read() + wg.Done() + }() go func() { c.process() wg.Done()