cluster/daemons: Close the datastore AFTER the DHT.

Avoids panics.

This also removes the abnormality of cluster closing a datastore
that it did not create.
This commit is contained in:
Hector Sanjuan 2020-04-02 16:29:41 +02:00
parent bebd1e168c
commit 65ad4bd632
4 changed files with 12 additions and 13 deletions

View File

@ -663,10 +663,10 @@ func (c *Cluster) Ready() <-chan struct{} {
// * Save peerstore with the current peers
// * Remove itself from consensus when LeaveOnShutdown is set
// * It Shutdowns all the components
// * Closes the datastore
// * Collects all goroutines
//
// Shutdown does not closes the libp2p host or the DHT.
// Shutdown does not close the libp2p host, the DHT, the datastore or
// generally anything that Cluster did not create.
func (c *Cluster) Shutdown(ctx context.Context) error {
_, span := trace.StartSpan(ctx, "cluster/Shutdown")
defer span.End()
@ -765,12 +765,6 @@ func (c *Cluster) Shutdown(ctx context.Context) error {
c.cancel()
c.wg.Wait()
// Cleanly close the datastore
if err := c.datastore.Close(); err != nil {
logger.Errorf("error closing Datastore: %s", err)
return err
}
c.shutdownB = true
close(c.doneCh)
return nil

View File

@ -399,7 +399,7 @@ func runCmd(c *cli.Context) error {
return cli.Exit(errors.Wrap(err, "error creating cluster peer"), 1)
}
return cmdutils.HandleSignals(ctx, cancel, cluster, host, dht)
return cmdutils.HandleSignals(ctx, cancel, cluster, host, dht, store)
}
// List

View File

@ -102,7 +102,7 @@ func daemon(c *cli.Context) error {
// will realize).
go bootstrap(ctx, cluster, bootstraps)
return cmdutils.HandleSignals(ctx, cancel, cluster, host, dht)
return cmdutils.HandleSignals(ctx, cancel, cluster, host, dht, store)
}
// createCluster creates all the necessary things to produce the cluster

View File

@ -13,12 +13,14 @@ import (
"syscall"
"time"
"github.com/ipfs/go-datastore"
ipfscluster "github.com/ipfs/ipfs-cluster"
ipfshttp "github.com/ipfs/ipfs-cluster/ipfsconn/ipfshttp"
host "github.com/libp2p/go-libp2p-host"
dht "github.com/libp2p/go-libp2p-kad-dht"
ma "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
// RandomizePorts replaces TCP and UDP ports with random, but valid port
@ -106,6 +108,7 @@ func HandleSignals(
cluster *ipfscluster.Cluster,
host host.Host,
dht *dht.IpfsDHT,
store datastore.Datastore,
) error {
signalChan := make(chan os.Signal, 20)
signal.Notify(
@ -123,9 +126,11 @@ func HandleSignals(
handleCtrlC(ctx, cluster, ctrlcCount)
case <-cluster.Done():
cancel()
dht.Close()
host.Close()
return nil
return multierr.Combine(
dht.Close(),
host.Close(),
store.Close(),
)
}
}
}