Commit ad201cd1 authored by aloknerurkar's avatar aloknerurkar Committed by GitHub

fix: shutdown only once (#1912)

parent 01a587d8
...@@ -103,6 +103,8 @@ type Bee struct { ...@@ -103,6 +103,8 @@ type Bee struct {
recoveryHandleCleanup func() recoveryHandleCleanup func()
listenerCloser io.Closer listenerCloser io.Closer
postageServiceCloser io.Closer postageServiceCloser io.Closer
shutdownInProgress bool
shutdownMutex sync.Mutex
} }
type Options struct { type Options struct {
...@@ -707,6 +709,15 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey, ...@@ -707,6 +709,15 @@ func NewBee(addr string, swarmAddress swarm.Address, publicKey ecdsa.PublicKey,
func (b *Bee) Shutdown(ctx context.Context) error { func (b *Bee) Shutdown(ctx context.Context) error {
var mErr 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 // halt kademlia while shutting down other
// components. // components.
b.topologyHalter.Halt() b.topologyHalter.Halt()
...@@ -714,7 +725,6 @@ func (b *Bee) Shutdown(ctx context.Context) error { ...@@ -714,7 +725,6 @@ func (b *Bee) Shutdown(ctx context.Context) error {
// halt p2p layer from accepting new connections // halt p2p layer from accepting new connections
// while shutting down other components // while shutting down other components
b.p2pHalter.Halt() b.p2pHalter.Halt()
// tryClose is a convenient closure which decrease // tryClose is a convenient closure which decrease
// repetitive io.Closer tryClose procedure. // repetitive io.Closer tryClose procedure.
tryClose := func(c io.Closer, errMsg string) { tryClose := func(c io.Closer, errMsg string) {
...@@ -851,6 +861,8 @@ type pidKiller struct { ...@@ -851,6 +861,8 @@ type pidKiller struct {
node *Bee node *Bee
} }
var ErrShutdownInProgress error = errors.New("shutdown in progress")
func (p *pidKiller) Shutdown(ctx context.Context) error { func (p *pidKiller) Shutdown(ctx context.Context) error {
err := p.node.Shutdown(ctx) err := p.node.Shutdown(ctx)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment