diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index daaa21984..e54f5edf2 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -442,7 +442,11 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode { // IsLeader check if the node is a leader or not by comparing the public key of // the node with the leader public key func (consensus *Consensus) IsLeader() bool { - consensus.mutex.RLock() + _ = utils.AssertNoLongerThan0(5*time.Second, func() error { + consensus.mutex.RLock() + return nil + }) + defer consensus.mutex.RUnlock() return consensus.isLeader() diff --git a/internal/utils/singleton.go b/internal/utils/singleton.go index 1b71e981b..9b94e8a35 100644 --- a/internal/utils/singleton.go +++ b/internal/utils/singleton.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "runtime/debug" "strconv" "sync" "time" @@ -213,3 +214,19 @@ func GetPort() int { } return 0 } + +func AssertNoLongerThan0[E any](t time.Duration, f func() E) E { + ch := make(chan E) + defer close(ch) + stack := debug.Stack() + go func() { + select { + case <-time.After(t): + panic("AssertNoLongerThan0: " + string(stack)) + case <-ch: + return + } + }() + + return f() +}