From f250adf4a025dcb64cb569a8cb26fa0443b56fae Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Sun, 14 Jul 2024 20:47:50 -0700 Subject: [PATCH] fix: possible race condition leading to rare panic in GetOperationEvents --- internal/api/backresthandler.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/internal/api/backresthandler.go b/internal/api/backresthandler.go index d47aefb6..1274925e 100644 --- a/internal/api/backresthandler.go +++ b/internal/api/backresthandler.go @@ -229,27 +229,17 @@ func (s *BackrestHandler) GetOperationEvents(ctx context.Context, req *connect.R s.oplog.Subscribe(&callback) defer s.oplog.Unsubscribe(&callback) - go func() { - for { - select { - case <-ctx.Done(): - return - case event := <-events: - if err := resp.Send(event); err != nil { - select { - case errChan <- errors.New("failed to send event"): - default: - } - } + for { + select { + case err := <-errChan: + return err + case <-ctx.Done(): + return nil + case event := <-events: + if err := resp.Send(event); err != nil { + return err } } - }() - - select { - case err := <-errChan: - return err - case <-ctx.Done(): - return nil } }