From f0e1df9bdc1abcc3be753ea89e609f199e4a3533 Mon Sep 17 00:00:00 2001 From: Vladimir Vershinin Date: Wed, 19 Jun 2024 16:20:34 +0300 Subject: [PATCH] Fix possible write to closed chan error Wait for connection pool serveHandlers goroutine to finish before resolving of the done chan. --- transport/connection_pool.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/transport/connection_pool.go b/transport/connection_pool.go index d9ae28e..5f7c261 100644 --- a/transport/connection_pool.go +++ b/transport/connection_pool.go @@ -58,9 +58,10 @@ type connectionPool struct { errs chan<- error cancel <-chan struct{} - done chan struct{} - hmess chan sip.Message - herrs chan error + done chan struct{} + hmess chan sip.Message + herrs chan error + srvhDone chan struct{} hwg sync.WaitGroup mu sync.RWMutex @@ -83,9 +84,10 @@ func NewConnectionPool( errs: errs, cancel: cancel, - done: make(chan struct{}), - hmess: make(chan sip.Message), - herrs: make(chan error), + done: make(chan struct{}), + hmess: make(chan sip.Message), + herrs: make(chan error), + srvhDone: make(chan struct{}), } pool.log = logger. @@ -200,6 +202,7 @@ func (pool *connectionPool) dispose() { close(pool.hmess) close(pool.herrs) + <-pool.srvhDone close(pool.done) } @@ -207,6 +210,8 @@ func (pool *connectionPool) serveHandlers() { pool.Log().Debug("begin serve connection handlers") defer pool.Log().Debug("stop serve connection handlers") + defer close(pool.srvhDone) + for { logger := pool.Log()