cmd/torrent/main.go | 18 ++++++++++++++---- diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 0e15eb0575faf7255fe1f7bb74b1f3440110a95f..b0beba21fec664ae536780bccb2d0dd55bf7b452 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -12,6 +12,7 @@ "strings" "syscall" "time" + "github.com/anacrolix/missinggo" "golang.org/x/xerrors" "github.com/anacrolix/log" @@ -163,12 +164,12 @@ } return *flags.Stats } -func exitSignalHandlers(client *torrent.Client) { +func exitSignalHandlers(notify *missinggo.SynchronizedEvent) { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) for { log.Printf("close signal received: %+v", <-c) - client.Close() + notify.Set() } } @@ -212,12 +213,21 @@ if flags.Quiet { clientConfig.Logger = log.Discard } + var stop missinggo.SynchronizedEvent + defer func() { + stop.Set() + }() + client, err := torrent.NewClient(clientConfig) if err != nil { return xerrors.Errorf("creating client: %v", err) } defer client.Close() - go exitSignalHandlers(client) + go exitSignalHandlers(&stop) + go func() { + <-stop.C() + client.Close() + }() // Write status on the root path on the default HTTP muxer. This will be bound to localhost // somewhere if GOPPROF is set, thanks to the envpprof import. @@ -238,7 +248,7 @@ return xerrors.New("y u no complete torrents?!") } if flags.Seed { outputStats(client) - select {} + <-stop.C() } outputStats(client) return nil