Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-pack/filebeat/input/httpjson: Close connections properly #39790

Merged
merged 7 commits into from
Jun 13, 2024

Conversation

shmsr
Copy link
Member

@shmsr shmsr commented Jun 3, 2024

Proposed commit message

Fix potential connection leaks in case of errors or when the response has been read but the connection remains unclosed.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Related issues

@shmsr shmsr requested a review from a team as a code owner June 3, 2024 20:01
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jun 3, 2024
@botelastic
Copy link

botelastic bot commented Jun 3, 2024

This pull request doesn't have a Team:<team> label.

@shmsr shmsr self-assigned this Jun 3, 2024
@shmsr shmsr marked this pull request as draft June 3, 2024 20:02
Copy link
Contributor

mergify bot commented Jun 3, 2024

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @shmsr? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-v8./d.0 is the label to automatically backport to the 8./d branch. /d is the digit

@shmsr shmsr requested a review from kush-elastic June 3, 2024 20:06
Comment on lines 678 to 682
defer func() {
if httpResp != nil && httpResp.Body != nil {
httpResp.Body.Close()
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callees here all follow the normal invariant that if err is nil, then the response is non-nil. So the condition can go away. If the response is non-nil, the body should be non-nil (this is documented as a property of http.Response values returned by http.Client and http.Transport and if we are not following that, it is a bug).

Suggested change
defer func() {
if httpResp != nil && httpResp.Body != nil {
httpResp.Body.Close()
}
}()
defer httpResp.Body.Close()

Actually, looking closer, I do not believe that this ever needs to happen; all responses come from httpClient.do which passes all bodies through drainBody which both completely drains (which is not done here) and closes it.

Copy link
Member Author

@shmsr shmsr Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Now that I look more closely, I see:

err = resp.Body.Close()
in getIdsFromResponses, which we can get rid of since we are already draining the body and closing it with drainBody?

I'll remove my changes as well.

x-pack/filebeat/input/httpjson/policy.go Show resolved Hide resolved
@efd6
Copy link
Contributor

efd6 commented Jun 4, 2024

Do we want a note in changelog dev?

@shmsr shmsr requested a review from efd6 June 4, 2024 10:07
@shmsr shmsr marked this pull request as ready for review June 5, 2024 06:16
@shmsr shmsr enabled auto-merge (squash) June 13, 2024 06:25
@shmsr shmsr merged commit ea9575a into elastic:main Jun 13, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug enhancement needs_team Indicates that the issue/PR needs a Team:* label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants