From e49ce483b025c60d11ce36e8d3512384d6b43fea Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 20 Nov 2023 17:04:16 +0100 Subject: [PATCH] fix: use 1 as sized channel Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- tm2/pkg/bft/consensus/common_test.go | 7 ++++++- tm2/pkg/bft/consensus/state_test.go | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tm2/pkg/bft/consensus/common_test.go b/tm2/pkg/bft/consensus/common_test.go index 3f5b3a55e95..4acc05c2438 100644 --- a/tm2/pkg/bft/consensus/common_test.go +++ b/tm2/pkg/bft/consensus/common_test.go @@ -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 { diff --git a/tm2/pkg/bft/consensus/state_test.go b/tm2/pkg/bft/consensus/state_test.go index 262bbbea254..1bcb3ec2128 100644 --- a/tm2/pkg/bft/consensus/state_test.go +++ b/tm2/pkg/bft/consensus/state_test.go @@ -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 {