Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed data race. #4559

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,18 @@ func (consensus *Consensus) selfCommit(payload []byte) error {
// NumSignaturesIncludedInBlock returns the number of signatures included in the block
func (consensus *Consensus) NumSignaturesIncludedInBlock(block *types.Block) uint32 {
count := uint32(0)
consensus.mutex.Lock()
members := consensus.Decider.Participants()
pubKeys := consensus.getPublicKeys()
consensus.mutex.Unlock()

// TODO(audit): do not reconstruct the Mask
mask := bls.NewMask(members)
err := mask.SetMask(block.Header().LastCommitBitmap())
if err != nil {
return count
}
for _, key := range consensus.GetPublicKeys() {
for _, key := range pubKeys {
if ok, err := mask.KeyEnabled(key.Bytes); err == nil && ok {
count++
}
Expand Down
8 changes: 4 additions & 4 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,19 +575,19 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.FBFTLog().IsBlockVerified(blk.Hash())); err != nil {
switch {
case errors.Is(err, core.ErrKnownBlock):
consensus.getLogger().Info().Msg("[preCommitAndPropose] Block already known")
consensus.GetLogger().Info().Msg("[preCommitAndPropose] Block already known")
default:
consensus.getLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain")
consensus.GetLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain")
return
}
}

consensus.mutex.Lock()
consensus.getLogger().Info().Msg("[preCommitAndPropose] Start consensus timer")
consensus.consensusTimeout[timeoutConsensus].Start()

// Send signal to Node to propose the new block for consensus
consensus.getLogger().Info().Msg("[preCommitAndPropose] sending block proposal signal")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetLogger() ?

Copy link
Contributor Author

@Frozen Frozen Feb 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we obtained lock above on line 584

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetLogger() has lock inside, so it will be deadlock.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetLogger is a public function, it should be improved to not have such a constraints. We can't make a function public and ask users to not use it in a certain conditions, right?


consensus.mutex.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we release lock exactly here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to excludes line consensus.ReadySignal(AsyncProposal) ? any reason for that?

consensus.ReadySignal(AsyncProposal)
}()

Expand Down
7 changes: 2 additions & 5 deletions core/blockchain_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,8 @@ func (bc *BlockChainImpl) InsertChain(chain types.Blocks, verifyHeaders bool) (i
}

prevHash := bc.CurrentBlock().Hash()
bc.chainmu.Lock()
defer bc.chainmu.Unlock()
n, events, logs, err := bc.insertChain(chain, verifyHeaders)
bc.PostChainEvents(events, logs)
if err == nil {
Expand Down Expand Up @@ -1699,9 +1701,6 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
}
}

bc.chainmu.Lock()
defer bc.chainmu.Unlock()

// A queued approach to delivering events. This is generally
// faster than direct delivery and requires much less mutex
// acquiring.
Expand Down Expand Up @@ -1801,9 +1800,7 @@ func (bc *BlockChainImpl) insertChain(chain types.Blocks, verifyHeaders bool) (i
// Prune in case non-empty winner chain
if len(winner) > 0 {
// Import all the pruned blocks to make the state available
bc.chainmu.Unlock()
_, evs, logs, err := bc.insertChain(winner, true /* verifyHeaders */)
bc.chainmu.Lock()
events, coalescedLogs = evs, logs

if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions p2p/stream/common/streammanager/streammanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func (sm *streamManager) loop() {
discCancel() // cancel last discovery
}
discCtx, discCancel = context.WithCancel(sm.ctx)
go func() {
discovered, err := sm.discoverAndSetupStream(discCtx)
go func(ctx context.Context) {
discovered, err := sm.discoverAndSetupStream(ctx)
if err != nil {
sm.logger.Err(err)
}
Expand All @@ -152,7 +152,7 @@ func (sm *streamManager) loop() {
sm.coolDown.UnSet()
}()
}
}()
}(discCtx)

case addStream := <-sm.addStreamCh:
err := sm.handleAddStream(addStream.st)
Expand Down