fix(core) check nil for _all_ owned resources
since construction can fail, and construction is non-trivial, it's probably safer to never assume resource exists. cc @jbenet @whyrusleeping
Brian Tiger Chow committed
Feb 6, 2015 at 11:22 UTC
8558838d00bd6c6e58630bd95b5b93db61da3585
1 file changed
+4
-5
core/core.go
+4
-5
@@ -280,17 +280,16 @@ func (n *IpfsNode) teardown() error {
280
log.Debug("core is shutting down...")
281
// owned objects are closed in this teardown to ensure that they're closed
282
// regardless of which constructor was used to add them to the node.
283
- closers := []io.Closer{
284
- n.Blocks,
285
- n.Exchange,
286
- n.Repo,
287
- }
283
+ var closers []io.Closer
284
addCloser := func(c io.Closer) { // use when field may be nil
285
if c != nil {
286
closers = append(closers, c)
287
}
288
}
289
290
+ addCloser(n.Blocks)
291
+ addCloser(n.Exchange)
292
+ addCloser(n.Repo)
293
addCloser(n.Bootstrapper)
294
if dht, ok := n.Routing.(*dht.IpfsDHT); ok {
295
addCloser(dht)