]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Don't call Client onClose handlers until torrent storages are closed
authorMatt Joiner <anacrolix@gmail.com>
Fri, 10 Sep 2021 04:15:45 +0000 (14:15 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 11 Sep 2021 01:39:33 +0000 (11:39 +1000)
client.go

index 0de33272c20fcb842da268c888ef0861debd3fc5..5ff6acc4539527314f20e328f9889e4a354743bc 100644 (file)
--- a/client.go
+++ b/client.go
@@ -419,18 +419,20 @@ 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() {
-       var closeGroup sync.WaitGroup // WaitGroup for any concurrent cleanup to complete before returning.
-       defer closeGroup.Wait()       // defer is LIFO. We want to Wait() after cl.unlock()
-       cl.lock()
-       defer cl.unlock()
        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)
        }
+       cl.unlock()
+       closeGroup.Wait() // defer is LIFO. We want to Wait() after cl.unlock()
+       cl.lock()
        for i := range cl.onClose {
                cl.onClose[len(cl.onClose)-1-i]()
        }
-       cl.event.Broadcast()
+       cl.unlock()
 }
 
 func (cl *Client) ipBlockRange(ip net.IP) (r iplist.Range, blocked bool) {