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 {
recoveryHandleCleanup func()
listenerCloser io.Closer
postageServiceCloser io.Closer
shutdownInProgress bool
shutdownMutex sync.Mutex
}
type Options struct {
......@@ -707,6 +709,15 @@ 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()
......@@ -714,7 +725,6 @@ func (b *Bee) Shutdown(ctx context.Context) error {
// 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) {
......@@ -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 {
......
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