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

Fix memory leak during signing #596

Merged
merged 1 commit into from
Jan 23, 2025
Merged

Fix memory leak during signing #596

merged 1 commit into from
Jan 23, 2025

Conversation

graebm
Copy link
Contributor

@graebm graebm commented Jan 21, 2025

Issue: (internal ticket V1637484435)

When doing SigV4A signing from aws-sdk-js-v3, memory usage gradually increases due to memory leaks.

Description of changes:
Fix leak that occurred if headers were queried repeatedly.

Future Work:
All native unit tests should check that they aren't leaking memory. I spent a few hours trying to add such tests, but couldn't get it working in a reliable way. I didn't want this to hold back the fix, so leaving it for another day. See these blog posts for hints on various techniques NodeJS has used for leak testing through the years: https://joyeecheung.github.io/blog/2024/03/17/memory-leak-testing-v8-node-js-1/, https://joyeecheung.github.io/blog/2024/03/17/memory-leak-testing-v8-node-js-2/

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Comment on lines +154 to +156
if (binding->node_external != NULL) {
napi_delete_reference(env, binding->node_external);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes another leak I found when searching for uses of napi_create_reference()/napi_delete_reference())

No one is using the NodeJS bindings of our HTTP classes, as far as I know...

Comment on lines +31 to +33
if (binding->node_external != NULL) {
napi_delete_reference(env, binding->node_external);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes another leak I found when searching for uses of napi_create_reference()/napi_delete_reference())

No one is using the NodeJS bindings of our HTTP classes, as far as I know...

@@ -115,20 +115,6 @@ struct aws_http_message *aws_napi_http_message_unwrap(napi_env env, napi_value j
* Constructor
**********************************************************************************************************************/

static void s_napi_http_request_finalize(napi_env env, void *finalize_data, void *finalize_hint) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There were 2 different finalizers for the same class. I combined them into 1.

(When this code was first written, the 2 different finalizers were necessary because the underlying C classes didn't have reference-counting. But now they do, so the weirdness the led to needing 2 different finalizers isn't necessary anymore)

Comment on lines +234 to 237
AWS_NAPI_ENSURE(env, napi_create_reference(env, result, 1, &binding->node_headers));
}

/* Store the value for later */
AWS_NAPI_ENSURE(env, napi_create_reference(env, result, 1, &binding->node_headers));

return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the leak. We were creating a new napi_ref each time the headers were queried. Now, we only create a napi_ref if we didn't already have one.

@@ -74,7 +74,7 @@ static void s_on_response_call(napi_env env, napi_value on_response, void *conte
}

/* clean up the response buffer */
aws_http_message_destroy(binding->response);
aws_http_message_release(binding->response);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this isn't a bugfix, just replacing a deprecated _destroy() function with a modern _release() function

@graebm graebm merged commit c4930d2 into main Jan 23, 2025
33 checks passed
@graebm graebm deleted the httpheaders-leak branch January 23, 2025 22:00
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