grab context of chain before calling isBootstrapped() to avoid race condition

This commit is contained in:
Dan Laine 2020-06-29 18:07:48 -04:00
parent 76d82288e1
commit ebf1ae152b
1 changed files with 13 additions and 10 deletions

View File

@ -119,7 +119,7 @@ type manager struct {
// Key: Chain's ID // Key: Chain's ID
// Value: The chain // Value: The chain
chains map[[32]byte]common.Engine chains map[[32]byte]*router.Handler
unblocked bool unblocked bool
blockedChains []ChainParameters blockedChains []ChainParameters
@ -138,7 +138,7 @@ func New(
decisionEvents *triggers.EventDispatcher, decisionEvents *triggers.EventDispatcher,
consensusEvents *triggers.EventDispatcher, consensusEvents *triggers.EventDispatcher,
db database.Database, db database.Database,
router router.Router, rtr router.Router,
net network.Network, net network.Network,
consensusParams avacon.Parameters, consensusParams avacon.Parameters,
validators validators.Manager, validators validators.Manager,
@ -152,7 +152,7 @@ func New(
timeoutManager.Initialize(requestTimeout) timeoutManager.Initialize(requestTimeout)
go log.RecoverAndPanic(timeoutManager.Dispatch) go log.RecoverAndPanic(timeoutManager.Dispatch)
router.Initialize(log, &timeoutManager, gossipFrequency, shutdownTimeout) rtr.Initialize(log, &timeoutManager, gossipFrequency, shutdownTimeout)
m := &manager{ m := &manager{
stakingEnabled: stakingEnabled, stakingEnabled: stakingEnabled,
@ -162,7 +162,7 @@ func New(
decisionEvents: decisionEvents, decisionEvents: decisionEvents,
consensusEvents: consensusEvents, consensusEvents: consensusEvents,
db: db, db: db,
chainRouter: router, chainRouter: rtr,
net: net, net: net,
timeoutManager: &timeoutManager, timeoutManager: &timeoutManager,
consensusParams: consensusParams, consensusParams: consensusParams,
@ -172,7 +172,7 @@ func New(
server: server, server: server,
keystore: keystore, keystore: keystore,
sharedMemory: sharedMemory, sharedMemory: sharedMemory,
chains: make(map[[32]byte]common.Engine), chains: make(map[[32]byte]*router.Handler),
} }
m.Initialize() m.Initialize()
return m return m
@ -462,7 +462,7 @@ func (m *manager) createAvalancheChain(
eng: &engine, eng: &engine,
}) })
} }
m.chains[ctx.ChainID.Key()] = &engine m.chains[ctx.ChainID.Key()] = handler
return nil return nil
} }
@ -554,15 +554,18 @@ func (m *manager) createSnowmanChain(
eng: &engine, eng: &engine,
}) })
} }
m.chains[ctx.ChainID.Key()] = &engine m.chains[ctx.ChainID.Key()] = handler
return nil return nil
} }
func (m *manager) IsBootstrapped(id ids.ID) bool { func (m *manager) IsBootstrapped(id ids.ID) bool {
if chain, exists := m.chains[id.Key()]; exists && chain.IsBootstrapped() { chain, exists := m.chains[id.Key()]
return true if !exists {
return false
} }
return false chain.Context().Lock.Lock()
defer chain.Context().Lock.Unlock()
return chain.Engine().IsBootstrapped()
} }
// Shutdown stops all the chains // Shutdown stops all the chains