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

using defer inside for loop could lead to leaks #116

Closed
wants to merge 1 commit into from

Conversation

halilylm
Copy link

@halilylm halilylm commented Feb 6, 2023

you create resources inside for loop and defer inside for loop, the defer function will execute after function returns, so you won't be able to clean resources inside for loop, it all will wait until function exists. I put close after for loop ended. so it will release the resources after for loop ends.

@halilylm halilylm marked this pull request as draft February 6, 2023 18:47
@wneessen
Copy link
Owner

wneessen commented Feb 7, 2023

Thanks for the PR @halilylm. I was thinking about this already when I ported it from the original net/smtp but left it for now. But it's probably a good idea to adress this directly. It looks like the tests on smtp are failing now though, since we are focing the close on something that might not always be "closeable". Can you check if you can make the tests working again? Otherwise I suggest to leave it as is since we are "only" talking about tests here.

@james-d-elliott
Copy link
Contributor

james-d-elliott commented Feb 8, 2023

You can fix this most likely another way by placing both the defer and the NewClient() (and anything that uses the client) calls within a function (anonymous or named) inside the for loop most likely; as the issue with for loops and defer are that defer executes at the end of the function call not the for loop.

@halilylm
Copy link
Author

halilylm commented Feb 8, 2023

Sorry I didn't have time to fix errors to pass tests that's why I put in draft. @james-d-elliott yes wrapping in a function could solve it, we can call defer in that function without waiting all for loop.

@wneessen
Copy link
Owner

@halilylm following up on this, do you think you'll have time to complete this or should I go ahead and work on a fix? I'm planning to release the next go-mail version soon and I think this would be a good addition to be added to the release. Let me know.

@halilylm
Copy link
Author

@wneessen hey sorry, I am too busy to make it. I'll leave it to you

@wneessen
Copy link
Owner

@wneessen hey sorry, I am too busy to make it. I'll leave it to you

All good. Thanks for the update and brining up this issue.

@wneessen
Copy link
Owner

Superseded by #122

@wneessen wneessen closed this Mar 15, 2023
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.

3 participants