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