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

fix: use sized 1 channel #7

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 16 additions & 1 deletion tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,29 @@ func validatePrevoteAndPrecommit(t *testing.T, cs *ConsensusState, thisRound, lo
}

func subscribeToVoter(cs *ConsensusState, addr crypto.Address) <-chan events.Event {
return events.SubscribeFiltered(cs.evsw, testSubscriber, func(event events.Event) bool {
ch := events.SubscribeFiltered(cs.evsw, testSubscriber, func(event events.Event) bool {
if vote, ok := event.(types.EventVote); ok {
if vote.Vote.ValidatorAddress == addr {
return true
}
}
return false
})

// This modification addresses the deadlock issue outlined in issue
// #1320. By creating a buffered channel, we ensure that events are
// consumed even if the main thread is blocked. This prevents the
// deadlock that occurred when eventSwitch.FireEvent was blocked due to
// no available consumers for the event.
testch := make(chan events.Event, 1)
go func() {
defer close(testch)
for evt := range ch {
testch <- evt
}
}()

return testch
}

// -------------------------------------------------------------------------------
Expand Down
20 changes: 19 additions & 1 deletion tm2/pkg/bft/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consensus
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -1780,5 +1781,22 @@ func TestStateOutputVoteStats(t *testing.T) {
}

func subscribe(evsw events.EventSwitch, protoevent events.Event) <-chan events.Event {
return events.SubscribeToEvent(evsw, testSubscriber, protoevent)
name := reflect.ValueOf(protoevent).Type().Name()
listenerID := fmt.Sprintf("%s-%s", testSubscriber, name)
ch := events.SubscribeToEvent(evsw, listenerID, protoevent)

// Similar to the change in common_test.go, this modification introduces
// a buffered channel and a separate goroutine for event consumption.
// This approach ensures that events are consumed asynchronously,
// thereby avoiding the deadlock situation described in the GitHub issue
// where the eventSwitch.FireEvent method was blocked.
testch := make(chan events.Event, 1)
go func() {
defer close(testch)
for evt := range ch {
testch <- evt
}
}()

return testch
}