}
}
-// 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
}