]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent: Synchronize stopping
authorMatt Joiner <anacrolix@gmail.com>
Sat, 4 Jan 2020 05:26:49 +0000 (16:26 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 4 Jan 2020 05:41:02 +0000 (16:41 +1100)
cmd/torrent/main.go

index 0e15eb0575faf7255fe1f7bb74b1f3440110a95f..b0beba21fec664ae536780bccb2d0dd55bf7b452 100644 (file)
@@ -12,6 +12,7 @@ import (
        "syscall"
        "time"
 
+       "github.com/anacrolix/missinggo"
        "golang.org/x/xerrors"
 
        "github.com/anacrolix/log"
@@ -163,12 +164,12 @@ func statsEnabled() bool {
        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 @@ func mainErr() error {
                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 @@ func mainErr() error {
        }
        if flags.Seed {
                outputStats(client)
-               select {}
+               <-stop.C()
        }
        outputStats(client)
        return nil