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 big memory leak on client disconnect #574

Closed
wants to merge 1 commit into from

Conversation

JorisHansMeijer
Copy link
Contributor

A pthread needs to be joined or detached to prevent memory leaks. Only when starting the server with the runInBackground set to true and using pthreads.

A pthread needs to be joined or detached to prevent memory leaks
@JorisHansMeijer
Copy link
Contributor Author

From the man page of pthread_join;

NOTES top

   After a successful call to pthread_join(), the caller is
   guaranteed that the target thread has terminated.  The caller may
   then choose to do any clean-up that is required after termination
   of the thread (e.g., freeing memory or other resources that were
   allocated to the target thread).

   Joining with a thread that has previously been joined results in
   undefined behavior.

   **Failure to join** with a thread that is joinable (i.e., one that is
   not detached), produces a "zombie thread".  Avoid doing this,
   since each zombie thread consumes some system resources, and when
   enough zombie threads have accumulated, it will no longer be
   possible to create new threads (or processes).

@bk138
Copy link
Member

bk138 commented May 25, 2023

Hi, thanks for the PR and sorry for the delay! 🙇

I think I understood the basic problem and what this PR fixes, I just have a few questions, marked in the PR's code.


#ifdef LIBVNCSERVER_HAVE_LIBPTHREAD
// We can't join this thread, detach it here so the associated memory will be feed
pthread_detach(cl->client_thread);
Copy link
Member

Choose a reason for hiding this comment

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

Two thoughts here:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. I am unsure this is more clear. But I think it will work. However it would be best to test first (as the other code was tested). For me it is at the moment not so easy to test,
  2. Sadly I have no knowledge of windows threads. A similar fix might be needed. The pull request however only fixes the pthread memory leak.

@bk138 bk138 added this to the Release 0.9.15 milestone May 25, 2023
@bk138 bk138 self-assigned this May 25, 2023
@JorisHansMeijer
Copy link
Contributor Author

So just to give some background.

I was working on a system, using libsvnserver in the threaded variation, and I was loosing memory (memory leak). So I started testing and noticed with htop on the server that, each time the client disconnects, I was loosing memory. So I focussed on libsvnserver and after a long time testing found that the memory leak didn't happen when I was using libsvnserver without threading.

So I focussed on threading, disabling code to find where the leak was. And in the end found the leak happened at thread ending. At that time I noticed that the leak happened at the end of the thread. So I investigated the thread and found that it wasn't joined at the end. Only when the server is stopped the, at that time, still running thread was joined.

At this point I knew what the problem was but I couldn't find a good place to join the client thread. So I decided it would be easier to detach the thread as a detached thread frees it's own memory (no join needed).

@bk138 bk138 closed this in 8560a5a Aug 5, 2023
@bk138 bk138 removed this from the Release 0.9.15 milestone Aug 5, 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.

Big memory leak, on client disconnect, in server that is started with the runInBackground argument set to TRUE
2 participants