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 on timed out connection attempt #278

Merged
merged 3 commits into from
Aug 23, 2019
Merged

Conversation

ColinSullivan1
Copy link
Member

Fix a memory leak upon a failed connection.

Note, upon a first failed connection there will be a bump in memory of a few Kb as the .NET runtime allocates objects required to create a socket. This is a one time allocation. With this fix, memory remains stable after that point.

In debugging this, some tasks associated with the TCP client connection were collected while others were not, so there was not a one-to-one leaked object per failed attempt. No leaked objects had references to, or were referenced by NATS.Client objects - at least that the memory profiler would report. I did not get to the bottom of that mystery, but quantitatively testing a loop of 1000 failed connection attempts measuring memory + object counts indicated this bug was fixed with the changes.

Resolves #236.

Signed-off-by: Colin Sullivan [email protected]

@watfordgnf
Copy link
Collaborator

We may not dispose of sslStream if there is an exception during makeTls:

https://github.com/nats-io/nats.net/blob/d9993456439c1704ca819033eac155308db2855c/NATS.Client/Conn.cs#L483-L507

@ColinSullivan1
Copy link
Member Author

Good catch, thanks @watfordgnf. I'll add that to this PR.

Copy link
Collaborator

@watfordgnf watfordgnf left a comment

Choose a reason for hiding this comment

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

LGTM pending CI.

@ColinSullivan1
Copy link
Member Author

ColinSullivan1 commented Aug 23, 2019

@watfordgnf, added one more small thing - nulled out the ssl stream after dispose for good measure. I think this should do it. Tests are green locally, will let you know when they're green for CI.

@ColinSullivan1
Copy link
Member Author

CI Is green.

@ColinSullivan1 ColinSullivan1 merged commit 130cc15 into master Aug 23, 2019
@ColinSullivan1 ColinSullivan1 deleted the conn_memleak branch September 27, 2019 01:31
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.

Memory leak on failed connection attempts.
2 participants