From 692a602b0c9e945c429d37eb9c5db4340b9b1b40 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Tue, 23 Sep 2014 05:03:38 -0700 Subject: [PATCH] core context + cancels Erroring out in core setup should cancel the context to ensure subsystems are shut down. This has to happen all over the place we use contexts. @perfmode @whyrusleeping --- core/core.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/core.go b/core/core.go index 51f5bf23aa3..a961f295ba7 100644 --- a/core/core.go +++ b/core/core.go @@ -65,12 +65,18 @@ type IpfsNode struct { // NewIpfsNode constructs a new IpfsNode based on the given config. func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { + // derive this from a higher context. + // cancel if we need to fail early. + ctx, cancel := context.WithCancel(context.TODO()) + if cfg == nil { + cancel() return nil, fmt.Errorf("configuration required") } d, err := makeDatastore(cfg.Datastore) if err != nil { + cancel() return nil, err } @@ -89,8 +95,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { ) if online { - // add protocol services here. - ctx := context.TODO() // derive this from a higher context. // when not online, don't need to parse private keys (yet) local, err := initIdentity(cfg) @@ -103,17 +107,21 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it if err := dhtService.Start(ctx); err != nil { + cancel() return nil, err } if err := exchangeService.Start(ctx); err != nil { + cancel() return nil, err } net, err = inet.NewIpfsNetwork(context.TODO(), local, &mux.ProtocolMap{ mux.ProtocolID_Routing: dhtService, mux.ProtocolID_Exchange: exchangeService, + // add protocol services here. }) if err != nil { + cancel() return nil, err } @@ -132,6 +140,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { // session that simply doesn't return blocks bs, err := bserv.NewBlockService(d, exchangeSession) if err != nil { + cancel() return nil, err }