]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Return errors from Client.Close
authorMatt Joiner <anacrolix@gmail.com>
Thu, 7 Oct 2021 02:31:08 +0000 (13:31 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 7 Oct 2021 02:31:08 +0000 (13:31 +1100)
client.go
client_test.go
cmd/torrent/main.go

index 5e058be8ced24098ce38807ea9ac36876d19714a..3cf07cf337db3a6618486189fdeb309c751f58bf 100644 (file)
--- 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) {
index 084c6b666f7ec467d6805092250f7b717aa34784..414aac0675a52a8d4390a2ed0bd5771f398ba686 100644 (file)
@@ -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) {
index a2a34f63afee574cb5aa846e060e4c94bb0eace5..dabccf9dfbefe733626c192aa5f841de7c3abf7d 100644 (file)
@@ -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