2
0
mirror of synced 2025-02-23 22:28:11 +00:00

Return errors from Client.Close

This commit is contained in:
Matt Joiner 2021-10-07 13:31:08 +11:00
parent 28e9064288
commit f47a12bd32
3 changed files with 10 additions and 6 deletions

View File

@ -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) {

View 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) {

View 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