From 7f3bf5c0fe90288de6dada5d489c8b974bae5b36 Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Tue, 4 Jun 2024 01:23:29 +0530 Subject: [PATCH 1/4] x-pack/filebeat/input/httpjson: Close connections properly --- x-pack/filebeat/input/httpjson/policy.go | 5 +++++ x-pack/filebeat/input/httpjson/request.go | 15 +++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/x-pack/filebeat/input/httpjson/policy.go b/x-pack/filebeat/input/httpjson/policy.go index 0c671cb85bbb..5c140eba6624 100644 --- a/x-pack/filebeat/input/httpjson/policy.go +++ b/x-pack/filebeat/input/httpjson/policy.go @@ -91,6 +91,11 @@ func (p *Policy) CustomRetryPolicy(ctx context.Context, resp *http.Response, err // errors and may relate to outages on the server side. This will catch // invalid response codes as well, like 0 and 999. if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) { + defer func() { + if resp.Body != nil { + resp.Body.Close() + } + }() return true, nil } diff --git a/x-pack/filebeat/input/httpjson/request.go b/x-pack/filebeat/input/httpjson/request.go index 3e63f0267162..6e45487f8b34 100644 --- a/x-pack/filebeat/input/httpjson/request.go +++ b/x-pack/filebeat/input/httpjson/request.go @@ -618,7 +618,6 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra err error urlCopy url.URL urlString string - httpResp *http.Response intermediateResps []*http.Response finalResps []*http.Response ) @@ -672,10 +671,16 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra } // collect data from new urls - httpResp, err = rf.collectResponse(ctx, chainTrCtx, r) + httpResp, err := rf.collectResponse(ctx, chainTrCtx, r) if err != nil { return -1, fmt.Errorf("failed to collect response: %w", err) } + defer func() { + if httpResp != nil && httpResp.Body != nil { + httpResp.Body.Close() + } + }() + // store data according to response type if i == len(r.requestFactories)-1 && len(ids) != 0 { finalResps = append(finalResps, httpResp) @@ -702,12 +707,6 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra n += p.eventCount() } - defer func() { - if httpResp != nil && httpResp.Body != nil { - httpResp.Body.Close() - } - }() - return n, nil } From 0df1aafc78293030164b009d9c2fc1b89f648a1f Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Tue, 4 Jun 2024 12:22:26 +0530 Subject: [PATCH 2/4] Address review comments --- x-pack/filebeat/input/httpjson/policy.go | 1 + x-pack/filebeat/input/httpjson/request.go | 8 ++------ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/x-pack/filebeat/input/httpjson/policy.go b/x-pack/filebeat/input/httpjson/policy.go index 5c140eba6624..ed7f25bf2c2c 100644 --- a/x-pack/filebeat/input/httpjson/policy.go +++ b/x-pack/filebeat/input/httpjson/policy.go @@ -93,6 +93,7 @@ func (p *Policy) CustomRetryPolicy(ctx context.Context, resp *http.Response, err if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) { defer func() { if resp.Body != nil { + io.Copy(io.Discard, resp.Body) resp.Body.Close() } }() diff --git a/x-pack/filebeat/input/httpjson/request.go b/x-pack/filebeat/input/httpjson/request.go index 6e45487f8b34..b15f3db51b10 100644 --- a/x-pack/filebeat/input/httpjson/request.go +++ b/x-pack/filebeat/input/httpjson/request.go @@ -618,6 +618,7 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra err error urlCopy url.URL urlString string + httpResp *http.Response intermediateResps []*http.Response finalResps []*http.Response ) @@ -671,15 +672,10 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra } // collect data from new urls - httpResp, err := rf.collectResponse(ctx, chainTrCtx, r) + httpResp, err = rf.collectResponse(ctx, chainTrCtx, r) if err != nil { return -1, fmt.Errorf("failed to collect response: %w", err) } - defer func() { - if httpResp != nil && httpResp.Body != nil { - httpResp.Body.Close() - } - }() // store data according to response type if i == len(r.requestFactories)-1 && len(ids) != 0 { From 3bd7d651b334c9736af0bdecda2dce910f1a4b48 Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Tue, 4 Jun 2024 14:02:05 +0530 Subject: [PATCH 3/4] Make linter happy --- x-pack/filebeat/input/httpjson/policy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/filebeat/input/httpjson/policy.go b/x-pack/filebeat/input/httpjson/policy.go index ed7f25bf2c2c..43360c1ed0ff 100644 --- a/x-pack/filebeat/input/httpjson/policy.go +++ b/x-pack/filebeat/input/httpjson/policy.go @@ -93,7 +93,7 @@ func (p *Policy) CustomRetryPolicy(ctx context.Context, resp *http.Response, err if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) { defer func() { if resp.Body != nil { - io.Copy(io.Discard, resp.Body) + _, _ = io.Copy(io.Discard, resp.Body) resp.Body.Close() } }() From 6e9e5f33f3ff6d2638215f4fe4896666080177f3 Mon Sep 17 00:00:00 2001 From: subham sarkar Date: Tue, 4 Jun 2024 14:02:18 +0530 Subject: [PATCH 4/4] Add CHANGELOG entry --- CHANGELOG-developer.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG-developer.next.asciidoc b/CHANGELOG-developer.next.asciidoc index cf378879b15e..5f448d950a1f 100644 --- a/CHANGELOG-developer.next.asciidoc +++ b/CHANGELOG-developer.next.asciidoc @@ -98,6 +98,7 @@ The list below covers the major changes between 7.0.0-rc2 and main only. - Fix copy arguments for strict aligned architectures. {pull}36976[36976] - Fix panic when more than 32767 pipeline clients are active. {issue}38197[38197] {pull}38556[38556] - Skip flakey metrics test on windows in filebeat httpjson input. {issue}39676[39676] {pull}39678[39678] +- Close connections properly in Filbeat's HTTPJSON input. {pull}39790[39790] ==== Added