From 99c273f4993b8fe6b8e98782bb61554416912c5c Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 20 Sep 2023 12:14:37 +0300 Subject: [PATCH] [#169] pool: Close inner pools during close routine Some apps do not reuse pool instance and expect that `pool.Close()` free resources. But it didn't actually close inner SDK clients, so it leads to goroutine leak in storage. Signed-off-by: Alex Vanin --- pool/mock_test.go | 4 ++++ pool/pool.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pool/mock_test.go b/pool/mock_test.go index 9df107d..be7d4a8 100644 --- a/pool/mock_test.go +++ b/pool/mock_test.go @@ -189,3 +189,7 @@ func (m *mockClient) restartIfUnhealthy(ctx context.Context) (healthy bool, chan } return } + +func (m *mockClient) close() error { + return nil +} diff --git a/pool/pool.go b/pool/pool.go index aebd5d1..e87cabb 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -78,6 +78,8 @@ type client interface { dial(ctx context.Context) error // see clientWrapper.restartIfUnhealthy. restartIfUnhealthy(ctx context.Context) (bool, bool) + // see clientWrapper.close. + close() error } // clientStatus provide access to some metrics for connection. @@ -1080,6 +1082,13 @@ func (c *clientWrapper) incRequests(elapsed time.Duration, method MethodIndex) { } } +func (c *clientWrapper) close() error { + if c.client != nil { + return c.client.Close() + } + return nil +} + func (c *clientStatusMonitor) handleError(ctx context.Context, st apistatus.Status, err error) error { if err != nil { if needCountError(ctx, err) { @@ -2766,6 +2775,13 @@ func (p *Pool) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error) { func (p *Pool) Close() { p.cancel() <-p.closedCh + + // close all clients + for _, pools := range p.innerPools { + for _, cli := range pools.clients { + _ = cli.close() + } + } } // SyncContainerWithNetwork applies network configuration received via