GUACAMOLE-221: Merge terminate keep-alive thread immediately upon guac_socket_free().

This commit is contained in:
Virtually Nick 2020-11-02 20:09:18 -05:00 committed by GitHub
commit 4d3280e817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,6 +44,8 @@ char __guac_socket_BASE64_CHARACTERS[64] = {
static void* __guac_socket_keep_alive_thread(void* data) { static void* __guac_socket_keep_alive_thread(void* data) {
int old_cancelstate;
/* Calculate sleep interval */ /* Calculate sleep interval */
struct timespec interval; struct timespec interval;
interval.tv_sec = GUAC_SOCKET_KEEP_ALIVE_INTERVAL / 1000; interval.tv_sec = GUAC_SOCKET_KEEP_ALIVE_INTERVAL / 1000;
@ -65,8 +67,11 @@ static void* __guac_socket_keep_alive_thread(void* data) {
} }
/* Sleep until next keep-alive check */ /* Sleep until next keep-alive check, but allow thread cancellation
* during that sleep */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
nanosleep(&interval, NULL); nanosleep(&interval, NULL);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
} }
@ -202,9 +207,11 @@ void guac_socket_free(guac_socket* socket) {
/* Mark as closed */ /* Mark as closed */
socket->state = GUAC_SOCKET_CLOSED; socket->state = GUAC_SOCKET_CLOSED;
/* Wait for keep-alive, if enabled */ /* Stop keep-alive thread, if enabled */
if (socket->__keep_alive_enabled) if (socket->__keep_alive_enabled) {
pthread_cancel(socket->__keep_alive_thread);
pthread_join(socket->__keep_alive_thread, NULL); pthread_join(socket->__keep_alive_thread, NULL);
}
free(socket); free(socket);
} }