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

feat: Fix FD leak in request #456

Merged
merged 4 commits into from
Dec 20, 2023
Merged

Conversation

astuyve
Copy link
Contributor

@astuyve astuyve commented Dec 20, 2023

What does this PR do?

Fully fixes the FD leak by discarding the response stream using resume()
We already discard the result of any GET or POST here, and always have (which we know from the types returning only a status code), so resume() is fine here.

I was under the impression that since we did not provide response.on('data', cb) that this clause wouldn't apply and the response stream would be ignored:

If no 'response' handler is added, then the response will be entirely discarded. However, if a 'response' event handler is added, then the data from the response object must be consumed, either by calling response.read() whenever there is a 'readable' event, or by adding a 'data' handler, or by calling the .resume() method. Until the data is consumed, the 'end' event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error.

but of course passing a callback function counts as a handler so in this case the must clause counts and the underlying readable stream was never consumed, causing the FD leak.

This code has been present for many years but only became an issue in Node20. For that, I'm not exactly clear. But I can confirm this PR fixes the issue.

Motivation

You can see the fix in this notebook

Testing Guidelines

Additional Notes

Types of Changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)

@astuyve astuyve requested a review from a team as a code owner December 20, 2023 19:45
@@ -61,7 +71,7 @@ function sendRequest(url: URL, options: RequestOptions, buffer?: Buffer): Promis
});
});

request.on("error", (error) => {
request.once("error", (error) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://nodejs.org/api/events.html#emitteronceeventname-listener

this just guarantees we don't leave an emitter dangling

Copy link
Contributor

@joeyzhao2018 joeyzhao2018 left a comment

Choose a reason for hiding this comment

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

Wow

@astuyve astuyve merged commit 6baae54 into main Dec 20, 2023
@astuyve astuyve deleted the aj/add-resume-to-fully-fix-fd-leak branch December 20, 2023 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants