From 8e0771c21825e9621f29d6c4018038b4505a59f4 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Tue, 24 Aug 2021 12:32:19 +0100 Subject: [PATCH] core/bloombits: avoid crash when storing errors of different type (#23437) This fixes a rare crash which could occur when two different errors happened in the same bloombits.MatcherSession. --- core/bloombits/matcher.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/core/bloombits/matcher.go b/core/bloombits/matcher.go index 927232be0..f2a8bda17 100644 --- a/core/bloombits/matcher.go +++ b/core/bloombits/matcher.go @@ -510,8 +510,9 @@ type MatcherSession struct { closer sync.Once // Sync object to ensure we only ever close once quit chan struct{} // Quit channel to request pipeline termination - ctx context.Context // Context used by the light client to abort filtering - err atomic.Value // Global error to track retrieval failures deep in the chain + ctx context.Context // Context used by the light client to abort filtering + err error // Global error to track retrieval failures deep in the chain + errLock sync.Mutex pend sync.WaitGroup } @@ -529,10 +530,10 @@ func (s *MatcherSession) Close() { // Error returns any failure encountered during the matching session. func (s *MatcherSession) Error() error { - if err := s.err.Load(); err != nil { - return err.(error) - } - return nil + s.errLock.Lock() + defer s.errLock.Unlock() + + return s.err } // allocateRetrieval assigns a bloom bit index to a client process that can either @@ -630,7 +631,9 @@ func (s *MatcherSession) Multiplex(batch int, wait time.Duration, mux chan chan result := <-request if result.Error != nil { - s.err.Store(result.Error) + s.errLock.Lock() + s.err = result.Error + s.errLock.Unlock() s.Close() } s.deliverSections(result.Bit, result.Sections, result.Bitsets)