client.go | 8 ++++++-- client_test.go | 4 ++-- cmd/torrent/main.go | 4 ++-- diff --git a/client.go b/client.go index 5e058be8ced24098ce38807ea9ac36876d19714a..3cf07cf337db3a6618486189fdeb309c751f58bf 100644 --- a/client.go +++ b/client.go @@ -422,13 +422,16 @@ } // 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 @@ for i := range cl.onClose { 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 084c6b666f7ec467d6805092250f7b717aa34784..414aac0675a52a8d4390a2ed0bd5771f398ba686 100644 --- a/client_test.go +++ b/client_test.go @@ -30,7 +30,7 @@ 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 @@ defer os.Chdir(origDir) 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 a2a34f63afee574cb5aa846e060e4c94bb0eace5..dabccf9dfbefe733626c192aa5f841de7c3abf7d 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -334,11 +334,11 @@ if err != nil { 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