Skip to content

Commit

Permalink
fix: use 1 as sized channel
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Nov 20, 2023
1 parent ea0f308 commit e49ce48
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,12 @@ func subscribeToVoter(cs *ConsensusState, addr crypto.Address) <-chan events.Eve
return false
})

testch := make(chan events.Event, 16)
// 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 {
Expand Down
7 changes: 6 additions & 1 deletion tm2/pkg/bft/consensus/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,12 @@ func subscribe(evsw events.EventSwitch, protoevent events.Event) <-chan events.E
listenerID := fmt.Sprintf("%s-%s", testSubscriber, name)
ch := events.SubscribeToEvent(evsw, listenerID, protoevent)

testch := make(chan events.Event, 16)
// 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 {
Expand Down

0 comments on commit e49ce48

Please sign in to comment.