]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix races using resources on Close
authorMatt Joiner <anacrolix@gmail.com>
Wed, 1 Dec 2021 07:24:17 +0000 (18:24 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 12 Dec 2021 04:01:50 +0000 (15:01 +1100)
client.go
tracker_scraper.go

index bd03da3561a4da8d8ebeb308b95ebbc6c42e4fb4..c491118b04d905413e2ec08556641d56ac774072 100644 (file)
--- a/client.go
+++ b/client.go
@@ -422,26 +422,23 @@ func (cl *Client) eachDhtServer(f func(DhtServer)) {
        }
 }
 
-// Stops the client. All connections to peers are closed and all activity will
-// come to a halt.
+// Stops the client. All connections to peers are closed and all activity will come to a halt.
 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 {
                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()
-       cl.lock()
        for i := range cl.onClose {
                cl.onClose[len(cl.onClose)-1-i]()
        }
+       cl.closed.Set()
        cl.unlock()
+       cl.event.Broadcast()
+       closeGroup.Wait() // defer is LIFO. We want to Wait() after cl.unlock()
        return
 }
 
index 9ec295893c2ed0993dad42284667a0bb3a6e29aa..93b3ebcab07127f0722bc406fc10ca3115a8e643 100644 (file)
@@ -81,6 +81,12 @@ func (me *trackerScraper) getIp() (ip net.IP, err error) {
                err = errors.New("no ips")
                return
        }
+       me.t.cl.rLock()
+       defer me.t.cl.rUnlock()
+       if me.t.cl.closed.IsSet() {
+               err = errors.New("client is closed")
+               return
+       }
        for _, ip = range ips {
                if me.t.cl.ipIsBlocked(ip) {
                        continue