Skip to content

Commit

Permalink
fix: shutdown only once (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
aloknerurkar authored Jun 4, 2021
1 parent 01a587d commit ad201cd
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ type Bee struct {
recoveryHandleCleanup func()
listenerCloser io.Closer
postageServiceCloser io.Closer
shutdownInProgress bool
shutdownMutex sync.Mutex
}

type Options struct {
Expand Down Expand Up @@ -707,14 +709,22 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
func (b *Bee) Shutdown(ctx context.Context) error {
var mErr error

// if a shutdown is already in process, return here
b.shutdownMutex.Lock()
if b.shutdownInProgress {
b.shutdownMutex.Unlock()
return ErrShutdownInProgress
}
b.shutdownInProgress = true
b.shutdownMutex.Unlock()

// halt kademlia while shutting down other
// components.
b.topologyHalter.Halt()

// halt p2p layer from accepting new connections
// while shutting down other components
b.p2pHalter.Halt()

// tryClose is a convenient closure which decrease
// repetitive io.Closer tryClose procedure.
tryClose := func(c io.Closer, errMsg string) {
Expand Down Expand Up @@ -851,6 +861,8 @@ type pidKiller struct {
node *Bee
}

var ErrShutdownInProgress error = errors.New("shutdown in progress")

func (p *pidKiller) Shutdown(ctx context.Context) error {
err := p.node.Shutdown(ctx)
if err != nil {
Expand Down

0 comments on commit ad201cd

Please sign in to comment.