From 24f3cb9dc378ad017c4704830b06dd11b126a8ce Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Mon, 25 Jul 2022 17:24:31 -0700 Subject: [PATCH] protect inner context.Context mis-use --- consumer/consumer.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/consumer/consumer.go b/consumer/consumer.go index a557ff9..76c7a5d 100644 --- a/consumer/consumer.go +++ b/consumer/consumer.go @@ -111,7 +111,17 @@ func (c *Consumer[P, S, T]) Start(ctx context.Context) (err error) { go func() { defer wg.Done() err := c.worker(ctx, ch) - if err != nil && !errors.Is(err, context.Canceled) { + if err != nil { + if errors.Is(err, context.Canceled) { + // double check it our context we passed in that got cancelled and not some inner + // one through mis-use. Protect people from themselves. + select { + case <-ctx.Done(): + return + default: + log.Fatal(errors.Wrap(err, "issue encountered processing Jobs. It appears to be a misuse of the context.Context.")) + } + } log.Fatal(errors.Wrap(err, "issue encountered processing Jobs")) } }()