From 1a983a4dbb1107c5ee8ddfbf43b022b71fa4ba95 Mon Sep 17 00:00:00 2001 From: Thanatat Tamtan Date: Mon, 25 May 2026 11:16:30 +0700 Subject: [PATCH] fix: make context cancel unconditional in flush MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The context cancel function was declared and deferred inside a conditional block. A defer inside a conditional is correct but subtle — particularly if the closure were later refactored to use named returns, the cancel call would silently move. Initialize cancel to a no-op func upfront so the defer is always unconditional and its scope is obvious at a glance. Co-Authored-By: Claude Sonnet 4.6 (1M context) --- quickwit.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quickwit.go b/quickwit.go index c86ffb9..7b73151 100644 --- a/quickwit.go +++ b/quickwit.go @@ -211,11 +211,11 @@ func (c *Client) loop() { } ctx := context.Background() + cancel := context.CancelFunc(func() {}) if t := c.getIngestTimeout(); t != 0 { - var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, t) - defer cancel() } + defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(buf.Bytes())) if err != nil { return false