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

Add missing Dispose() calls #1552

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

brianpursley
Copy link
Contributor

@brianpursley brianpursley commented Apr 29, 2024

While not recommended, if you repeatedly create a Kubernetes client inside of a loop, there appears to be a memory leak, even if you dispose it.

This PR adds the following to fix this:

  • Adds .Dispose() for FirstMessageHandler
  • Adds .Dispose() for HttpClientHandler
  • A few other minor changes in the Dispose() method
  • Adds Using for httpRequest (while unrelated to the Kubernetes client lifecycle, I believe this should also be disposed)

Possible fix for #1539 (I suspect that issue is not related only to aot).

Test Results

I tested this using the following console program and running in Rider using memory profiling (sampled allocations):

using k8s;

var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();

for (var i = 0; i < 1000; i++)
{
    using var client = new Kubernetes(config);
    var list = client.CoreV1.ListNamespacedPod("kube-system");
    Console.WriteLine(string.Join(", ", list.Items.Select(x => x.Metadata.Name)));
}

Before the fix

Memory increases slowly, eventually reaching ~163MB by the end of the program:
BeforeFix1

After the fix

Memory holds stead around ~123MB:
AfterFix1

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 29, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @brianpursley!

It looks like this is your first PR to kubernetes-client/csharp 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/csharp has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Apr 29, 2024
@brianpursley
Copy link
Contributor Author

brianpursley commented Apr 29, 2024

Hmm, i think I can't do this:

using var httpRequest = new HttpRequestMessage

... because this is not awaited ...

return SendRequestRaw("", httpRequest, cancellationToken);

So the using disposes httpRequest before SendRequestRaw has executed.

SendRequest should probably use async/await, but I think that is a different problem. I will update this PR to remove that change.

@brianpursley
Copy link
Contributor Author

OK, removed the using change. I ran the profile again and it was unaffected, so maybe that change doesn't really make a difference anyway.

@tg123
Copy link
Member

tg123 commented Apr 29, 2024

Good catch

/LGTM

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 29, 2024
@codecov-commenter
Copy link

codecov-commenter commented Apr 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (master@3dae1cf). Click here to learn what that means.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1552   +/-   ##
=========================================
  Coverage          ?   61.50%           
=========================================
  Files             ?      103           
  Lines             ?     3068           
  Branches          ?      639           
=========================================
  Hits              ?     1887           
  Misses            ?     1181           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brianpursley, tg123

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 29, 2024
@k8s-ci-robot k8s-ci-robot merged commit b50aed2 into kubernetes-client:master Apr 29, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants