From 3e2bdcdd0aab8906d1be5354e36427ee6a1ee191 Mon Sep 17 00:00:00 2001 From: Phu Ngo <12547020+NgoKimPhu@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:09:14 +0700 Subject: [PATCH] fix: use background ctx on close as task from closed chan is nil --- batcher.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/batcher.go b/batcher.go index 3a57835..f045605 100644 --- a/batcher.go +++ b/batcher.go @@ -186,14 +186,16 @@ func (b *ChanBatcher[T, R]) worker() { go b.batchFnWithRecover(tasks) tasks = tasks[:0:0] case task, ok := <-b.taskCh: - ctx := task.Ctx() if !ok { - klog.Debugf(ctx, "ChanBatcher.worker|closed|%d tasks", len(tasks)) + ctx := context.Background() if len(tasks) > 0 { + ctx = tasks[0].Ctx() go b.batchFnWithRecover(tasks) } + klog.Debugf(ctx, "ChanBatcher.worker|closed|%d tasks", len(tasks)) return } + ctx := task.Ctx() if !task.IsDone() { select { case <-ctx.Done():