From f2718c32802400a1723c7459747b977988c56aae Mon Sep 17 00:00:00 2001 From: Tiago Queiroz Date: Mon, 16 Sep 2024 16:58:33 -0400 Subject: [PATCH] Cancel context before replacing it --- libbeat/esleg/eslegclient/connection.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libbeat/esleg/eslegclient/connection.go b/libbeat/esleg/eslegclient/connection.go index 11b5b8530df8..5284c9f288e1 100644 --- a/libbeat/esleg/eslegclient/connection.go +++ b/libbeat/esleg/eslegclient/connection.go @@ -288,6 +288,10 @@ func (conn *Connection) Connect() error { conn.log = logp.NewLogger("esclientleg") } + // Cancel the old context before replacing it. Because this Connection + // is not goroutine safe, there should be no in-flight request using + // this context, hence it's safe to cancel it. + conn.cancelReqs() conn.reqsContext, conn.cancelReqs = context.WithCancel(context.Background()) if err := conn.getVersion(); err != nil {