amendment to core context + cancels
Brian Tiger Chow committed
Sep 23, 2014 at 12:47 UTC
508a5b7a94d9dd5592b82764f6de72aa6e189e53
1 file changed
+7
-7
core/core.go
+7
-7
@@ -68,15 +68,19 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
68
// derive this from a higher context.
69
// cancel if we need to fail early.
70
ctx, cancel := context.WithCancel(context.TODO())
71
+ success := false // flip to true after all sub-system inits succeed
72
+ defer func() {
73
+ if !success {
74
+ cancel()
75
+ }
76
+ }()
77
78
if cfg == nil {
73
- cancel()
79
return nil, fmt.Errorf("configuration required")
80
}
81
82
d, err := makeDatastore(cfg.Datastore)
83
if err != nil {
79
- cancel()
84
return nil, err
85
}
86
@@ -99,7 +103,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
103
// when not online, don't need to parse private keys (yet)
104
local, err := initIdentity(cfg)
105
if err != nil {
102
- cancel()
106
return nil, err
107
}
108
@@ -107,11 +110,9 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
110
exchangeService := netservice.NewService(nil) // nil handler for now, need to patch it
111
112
if err := dhtService.Start(ctx); err != nil {
110
- cancel()
113
return nil, err
114
}
115
if err := exchangeService.Start(ctx); err != nil {
114
- cancel()
116
return nil, err
117
}
118
@@ -121,7 +122,6 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
122
// add protocol services here.
123
})
124
if err != nil {
124
- cancel()
125
return nil, err
126
}
127
@@ -140,12 +140,12 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
140
// session that simply doesn't return blocks
141
bs, err := bserv.NewBlockService(d, exchangeSession)
142
if err != nil {
143
- cancel()
143
return nil, err
144
}
145
146
dag := &merkledag.DAGService{Blocks: bs}
147
148
+ success = true
149
return &IpfsNode{
150
Config: cfg,
151
Peerstore: peerstore,