From ad82e28ba118160c0a577e35c21774726b182b6b Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 28 Jun 2024 09:29:48 -0400 Subject: [PATCH 1/2] Avoid leaking timeout timer channels Signed-off-by: Jordan Liggitt --- connection.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/connection.go b/connection.go index d649ecc..1b534e8 100644 --- a/connection.go +++ b/connection.go @@ -712,7 +712,9 @@ func (s *Connection) shutdown(closeTimeout time.Duration) { var timeout <-chan time.Time if closeTimeout > time.Duration(0) { - timeout = time.After(closeTimeout) + timer := time.NewTimer(closeTimeout) + defer timer.Stop() + timeout = timer.C } streamsClosed := make(chan bool) @@ -806,7 +808,9 @@ func (s *Connection) CloseWait() error { func (s *Connection) Wait(waitTimeout time.Duration) error { var timeout <-chan time.Time if waitTimeout > time.Duration(0) { - timeout = time.After(waitTimeout) + timer := time.NewTimer(waitTimeout) + defer timer.Stop() + timeout = timer.C } select { From 3f1023d13c03a01e0adc50f94b5d693232ca9aba Mon Sep 17 00:00:00 2001 From: Jordan Liggitt Date: Fri, 28 Jun 2024 09:49:20 -0400 Subject: [PATCH 2/2] Shorten timeout for unhandled error to 1 second Signed-off-by: Jordan Liggitt --- connection.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/connection.go b/connection.go index 1b534e8..1394d0a 100644 --- a/connection.go +++ b/connection.go @@ -741,7 +741,15 @@ func (s *Connection) shutdown(closeTimeout time.Duration) { } if err != nil { - duration := 10 * time.Minute + // default to 1 second + duration := time.Second + // if a closeTimeout was given, use that, clipped to 1s-10m + if closeTimeout > time.Second { + duration = closeTimeout + } + if duration > 10*time.Minute { + duration = 10 * time.Minute + } timer := time.NewTimer(duration) defer timer.Stop() select {