From 9b7b5ff6e3dac4e7106b59117a2c9c602ef79619 Mon Sep 17 00:00:00 2001 From: Christian Beier Date: Wed, 30 Aug 2023 09:20:25 +0200 Subject: [PATCH] Revert "libvncserver: detach client thread before ending it" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 8560a5a72d76fc3ab3484ca41f604116807f34e8. It caused several crashes when ending a server: client threads would sometimes linger around until after the server was stopped and its rfbScreen internals free()ed, with these client threads trying to access rfbScreen and friends: 💣. Reopens #570 --- src/libvncserver/main.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libvncserver/main.c b/src/libvncserver/main.c index b2dbe4cc..1efa8387 100644 --- a/src/libvncserver/main.c +++ b/src/libvncserver/main.c @@ -610,10 +610,6 @@ clientInput(void *data) rfbClientConnectionGone(cl); - /* We can't reliably join this thread, so detach it here - so the associated memory will be freed by the OS. */ - THREAD_DETACH(cl->client_thread); - return THREAD_ROUTINE_RETURN_VALUE; } @@ -1208,7 +1204,10 @@ void rfbShutdownServer(rfbScreenInfoPtr screen,rfbBool disconnectClients) { } #ifdef LIBVNCSERVER_HAVE_LIBPTHREAD - if(!currentCl->screen->backgroundLoop) { + if(currentCl->screen->backgroundLoop) { + /* Wait for threads to finish. The thread has already been pipe-notified by rfbCloseClient() */ + pthread_join(currentCl->client_thread, NULL); + } else { /* In threaded mode, rfbClientConnectionGone() is called by the client-to-server thread. Only need to call this here for non-threaded mode.