Skip to content

Commit

Permalink
Fix a deadlock during warpsync and publishing commitment.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrm50 committed Apr 2, 2024
1 parent 44acb4d commit bda5f5e
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions pkg/protocol/commitments.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,28 @@ func (c *Commitments) initRequester() (shutdown func()) {
// publishRootCommitment publishes the root commitment of the main engine.
func (c *Commitments) publishRootCommitment(mainChain *Chain, mainEngine *engine.Engine) func() {
return mainEngine.RootCommitment.OnUpdate(func(_ *model.Commitment, rootCommitment *model.Commitment) {
publishedCommitment, published, err := c.publishCommitment(rootCommitment)
if err != nil {
c.LogError("failed to publish new root commitment", "id", rootCommitment.ID(), "error", err)
// Use workerpool to avoid a deadlock when
c.workerPool.Submit(func() {
publishedCommitment, published, err := c.publishCommitment(rootCommitment)
if err != nil {
c.LogError("failed to publish new root commitment", "id", rootCommitment.ID(), "error", err)

return
}
return
}

publishedCommitment.IsRoot.Set(true)
if published {
publishedCommitment.Chain.Set(mainChain)
}
publishedCommitment.IsRoot.Set(true)
if published {
publishedCommitment.Chain.Set(mainChain)
}

// Update the forking point of a chain only if the root is empty or root belongs to the main chain or the published commitment is on the main chain.
// to avoid updating ForkingPoint of the new mainChain into the past.
if c.Root.Get() == nil || c.Root.Get().Chain.Get() == mainChain || publishedCommitment.Chain.Get() == mainChain {
mainChain.ForkingPoint.Set(publishedCommitment)
}
// Update the forking point of a chain only if the root is empty or root belongs to the main chain or the published commitment is on the main chain.
// to avoid updating ForkingPoint of the new mainChain into the past.
if c.Root.Get() == nil || c.Root.Get().Chain.Get() == mainChain || publishedCommitment.Chain.Get() == mainChain {
mainChain.ForkingPoint.Set(publishedCommitment)
}

c.Root.Set(publishedCommitment)
c.Root.Set(publishedCommitment)
})
})
}

Expand Down

0 comments on commit bda5f5e

Please sign in to comment.