From f47a12bd321ddc94ea6950db36bf4544e15e60da Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 7 Oct 2021 13:31:08 +1100 Subject: [PATCH] Return errors from Client.Close --- client.go | 8 ++++++-- client_test.go | 4 ++-- cmd/torrent/main.go | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 5e058be8..3cf07cf3 100644 --- a/client.go +++ b/client.go @@ -422,13 +422,16 @@ func (cl *Client) eachDhtServer(f func(DhtServer)) { // Stops the client. All connections to peers are closed and all activity will // come to a halt. -func (cl *Client) Close() { +func (cl *Client) Close() (errs []error) { cl.closed.Set() var closeGroup sync.WaitGroup // For concurrent cleanup to complete before returning cl.lock() cl.event.Broadcast() for _, t := range cl.torrents { - t.close(&closeGroup) + err := t.close(&closeGroup) + if err != nil { + errs = append(errs, err) + } } cl.unlock() closeGroup.Wait() // defer is LIFO. We want to Wait() after cl.unlock() @@ -437,6 +440,7 @@ func (cl *Client) Close() { cl.onClose[len(cl.onClose)-1-i]() } cl.unlock() + return } func (cl *Client) ipBlockRange(ip net.IP) (r iplist.Range, blocked bool) { diff --git a/client_test.go b/client_test.go index 084c6b66..414aac06 100644 --- a/client_test.go +++ b/client_test.go @@ -30,7 +30,7 @@ import ( func TestClientDefault(t *testing.T) { cl, err := NewClient(TestingConfig(t)) require.NoError(t, err) - cl.Close() + require.Empty(t, cl.Close()) } func TestClientNilConfig(t *testing.T) { @@ -40,7 +40,7 @@ func TestClientNilConfig(t *testing.T) { os.Chdir(t.TempDir()) cl, err := NewClient(nil) require.NoError(t, err) - cl.Close() + require.Empty(t, cl.Close()) } func TestAddDropTorrent(t *testing.T) { diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index a2a34f63..dabccf9d 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -334,11 +334,11 @@ func downloadErr(flags downloadFlags) error { return fmt.Errorf("creating client: %w", err) } var clientClose sync.Once //In certain situations, close was being called more than once. - defer clientClose.Do(client.Close) + defer clientClose.Do(func() { client.Close() }) go exitSignalHandlers(&stop) go func() { <-stop.C() - clientClose.Do(client.Close) + clientClose.Do(func() { client.Close() }) }() // Write status on the root path on the default HTTP muxer. This will be bound to localhost -- 2.44.0