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

HTTPClient needs to be shutdown after use to avoid memory leak #4

Merged
merged 4 commits into from
Jul 13, 2024
Merged

HTTPClient needs to be shutdown after use to avoid memory leak #4

merged 4 commits into from
Jul 13, 2024

Conversation

ronnybremer
Copy link
Contributor

@ronnybremer
Copy link
Contributor Author

Fixes #3

Copy link
Owner

@sliemeobn sliemeobn left a comment

Choose a reason for hiding this comment

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

Thanks for the PR and for bringing up this issue!

However, I am afraid if we want to clean this up for good, we'd need to change the "exiting" behavior as well (see comment below)

@@ -48,3 +50,5 @@ while true {

try await Task.sleep(nanoseconds: 3_000_000_000)
}

try httpClient.syncShutdown()
Copy link
Owner

Choose a reason for hiding this comment

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

This is never executed. The while loop above only exits...

The thing is that is technically completely unnecessary to shutdown the client before terminating anyway (so I did not want to include it in the example) - but having the debug build assertion sure is not nice either...

Also, since we are in an async context, I think this would result in a warning too, right? (we'd have to use .shutdown().get() or something I guess)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Absolutely right, I fixed the spots.

.syncShutdown() doesn't return anything, it throws if an error occurs. So I think we are good with that call.

Not really recommended programming, but it is to demonstrate the shutdown of the HTTPClient before terminating the program.
@ronnybremer
Copy link
Contributor Author

However, I am afraid if we want to clean this up for good, we'd need to change the "exiting" behavior as well (see comment below)

Quite right. It's always a struggle with examples, as they are for demonstration and not really high quality code.

Copy link
Owner

@sliemeobn sliemeobn left a comment

Choose a reason for hiding this comment

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

I don't want to be too pedantic on a silly little example like this, but I think it reads less cleanly than before.

Would you mind refactoring the while-loop to work without exit and have a single shutdown.

Maybe just wrap whole thing in a function that returns properly?

@ronnybremer
Copy link
Contributor Author

I don't want to be too pedantic on a silly little example like this, but I think it reads less cleanly than before.

Would you mind refactoring the while-loop to work without exit and have a single shutdown.

What do you think, is this better?

@ronnybremer
Copy link
Contributor Author

ping @sliemeobn

@sliemeobn
Copy link
Owner

sorry, I did not notice this last week. if that helps with the getting a proper error out instead of an asserting, it looks much better, yes. thanks!

one tiny last nit: could you remove the comment? it's just not really true that not shutting down causes a memory leak in an app like this - the process terminates anyway. even the NIO team are saying how this is a bit misunderstood.

@ronnybremer
Copy link
Contributor Author

one tiny last nit: could you remove the comment? it's just not really true that not shutting down causes a memory leak in an app like this - the process terminates anyway. even the NIO team are saying how this is a bit misunderstood.

I actually would prefer not to remove it. The comment is not really meaningful in this example, I agree, however an important detail for someone using the library for the first time. Otherwise they might miss that part.

What did the NIO team say about closing the client? From what I remember it was explicitly documented there that it needs to be closed.

@sliemeobn
Copy link
Owner

I hope you don't mind, but I think i fixed it for the better. thank you for your patience.

@sliemeobn sliemeobn merged commit d436be5 into sliemeobn:main Jul 13, 2024
1 check passed
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