From 47ab16c2bf7dd92fe616be7e614c3ddb72003f5a Mon Sep 17 00:00:00 2001 From: Alejandro Durante Date: Mon, 9 May 2022 20:24:36 -0300 Subject: [PATCH] Prevent NPE in GroupContext --- group_blackbox_test.go | 15 +++++++++++++++ pond.go | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/group_blackbox_test.go b/group_blackbox_test.go index 306f832..0f04f11 100644 --- a/group_blackbox_test.go +++ b/group_blackbox_test.go @@ -105,3 +105,18 @@ func TestGroupContextWithError(t *testing.T) { assertEqual(t, int32(5), atomic.LoadInt32(&startedCount)) assertEqual(t, int32(4), atomic.LoadInt32(&doneCount)) } + +func TestGroupContextWithNilContext(t *testing.T) { + + pool := pond.New(3, 100) + + var thrownPanic interface{} + func() { + defer func() { + thrownPanic = recover() + }() + pool.GroupContext(nil) + }() + + assertEqual(t, "a non-nil context needs to be specified when using GroupContext", thrownPanic) +} diff --git a/pond.go b/pond.go index d025b12..307d710 100644 --- a/pond.go +++ b/pond.go @@ -492,6 +492,11 @@ func (p *WorkerPool) Group() *TaskGroup { // The derived Context is canceled the first time a function submitted to the group // returns a non-nil error or the first time Wait returns, whichever occurs first. func (p *WorkerPool) GroupContext(ctx context.Context) (*TaskGroupWithContext, context.Context) { + + if ctx == nil { + panic("a non-nil context needs to be specified when using GroupContext") + } + ctx, cancel := context.WithCancel(ctx) return &TaskGroupWithContext{ TaskGroup: TaskGroup{