Skip to content

Commit

Permalink
Merge pull request #7058 from ipfs/fix/shutdown-errors
Browse files Browse the repository at this point in the history
fix: get rid of shutdown errors
  • Loading branch information
Stebalien committed Mar 30, 2020
2 parents 474ade1 + efdb8db commit 8f623c9
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"sync"
"time"

"github.com/ipfs/go-ipfs/core/bootstrap"
"github.com/ipfs/go-ipfs/core/node"
Expand All @@ -11,10 +12,26 @@ import (
"go.uber.org/fx"
)

// from https://stackoverflow.com/a/59348871
type valueContext struct {
context.Context
}

func (valueContext) Deadline() (deadline time.Time, ok bool) { return }
func (valueContext) Done() <-chan struct{} { return nil }
func (valueContext) Err() error { return nil }

type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refactor the constructor interface

// NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
// save this context as the "lifetime" ctx.
lctx := ctx

// derive a new context that ignores cancellations from the lifetime ctx.
ctx, cancel := context.WithCancel(valueContext{ctx})

// add a metrics scope.
ctx = metrics.CtxScope(ctx, "ipfs")

n := &IpfsNode{
Expand All @@ -33,18 +50,27 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
n.stop = func() error {
once.Do(func() {
stopErr = app.Stop(context.Background())
if stopErr != nil {
log.Error("failure on stop: ", stopErr)
}
// Cancel the context _after_ the app has stopped.
cancel()
})
return stopErr
}
n.IsOnline = cfg.Online

go func() {
// Note that some services use contexts to signal shutting down, which is
// very suboptimal. This needs to be here until that's addressed somehow
<-ctx.Done()
err := n.stop()
if err != nil {
log.Error("failure on stop: ", err)
// Shut down the application if the lifetime context is canceled.
// NOTE: we _should_ stop the application by calling `Close()`
// on the process. But we currently manage everything with contexts.
select {
case <-lctx.Done():
err := n.stop()
if err != nil {
log.Error("failure on stop: ", err)
}
case <-ctx.Done():
}
}()

Expand Down

0 comments on commit 8f623c9

Please sign in to comment.