From a04c427c3ac74398fc0cfdcec96d0d4ddfca5cae Mon Sep 17 00:00:00 2001 From: Toshihito Kikuchi Date: Mon, 25 Sep 2023 12:48:28 -0700 Subject: [PATCH] core/bloombits: fix deadlock when matcher session hits an error When MatcherSession encounters an error, it attempts to close the session. Closing waits for all goroutines to finish, including the 'distributor'. However, the distributor will not exit until all requests have returned. This patch fixes the issue by delivering the (empty) result to the distributor before calling Close(). --- core/bloombits/matcher.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/bloombits/matcher.go b/core/bloombits/matcher.go index 5d95930bf1..2c808dd049 100644 --- a/core/bloombits/matcher.go +++ b/core/bloombits/matcher.go @@ -633,13 +633,16 @@ func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan request <- &Retrieval{Bit: bit, Sections: sections, Context: s.ctx} result := <-request + + // Deliver a result before s.Close() to avoid a deadlock + s.deliverSections(result.Bit, result.Sections, result.Bitsets) + if result.Error != nil { s.errLock.Lock() s.err = result.Error s.errLock.Unlock() s.Close() } - s.deliverSections(result.Bit, result.Sections, result.Bitsets) } } }