From bab7e6f22e761cda2eabc0bff2fb63dcfa315876 Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Tue, 12 Nov 2024 09:12:41 -0500 Subject: [PATCH 1/2] fix: tid set to null --- packages/c/srv/src/srv.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/c/srv/src/srv.c b/packages/c/srv/src/srv.c index 781193be7..18b7deaed 100644 --- a/packages/c/srv/src/srv.c +++ b/packages/c/srv/src/srv.c @@ -269,8 +269,8 @@ int socket_to_socket(const srv_params_t *params, const char *auth_string, chunke int fds[2], tidx; int exit_res = 0; - pthread_t threads[2]; - pthread_t tid = NULL; + pthread_t threads[2], tid; + bool cancel_first = false; pipe(fds); srv_link_sides(&sides[0], &sides[1], fds); @@ -298,7 +298,7 @@ int socket_to_socket(const srv_params_t *params, const char *auth_string, chunke res = pthread_create(&threads[1], NULL, srv_side_handle, &sides[1]); if (res != 0) { - atlogger_log(TAG, ERROR, "Failed to create thread: 1\n"); + cancel_first = true; exit_res = res; goto cancel; } @@ -316,10 +316,13 @@ int socket_to_socket(const srv_params_t *params, const char *auth_string, chunke read(fds[0], &tid, sizeof(pthread_t)); atlogger_log(TAG, DEBUG, "Joining exited thread\n"); + + // When a thread exits, join it. res = pthread_join(tid, (void *)&retval); cancel: - if (pthread_equal(threads[0], tid) > 0) { + // Then figure out which thread didn't close + if (!cancel_first && pthread_equal(threads[0], tid) > 0) { // If threads[0] exited normally then we will cancel threads[1] // In all other cases, cancel threads[0] (could be because threads[1] exited or errored) tidx = 1; @@ -327,6 +330,7 @@ int socket_to_socket(const srv_params_t *params, const char *auth_string, chunke tidx = 0; } + // Then cancel the other thread atlogger_log(TAG, DEBUG, "Cancelling remaining open thread: %d\n", tidx); if (pthread_cancel(threads[tidx]) != 0) { atlogger_log(TAG, WARN, "Failed to cancel thread: %d\n", tidx); From 202c086b2b01d45b30218830b00e49203e901d6d Mon Sep 17 00:00:00 2001 From: xavierchanth Date: Tue, 12 Nov 2024 09:18:44 -0500 Subject: [PATCH 2/2] fix: restore a log message --- packages/c/srv/src/srv.c | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/c/srv/src/srv.c b/packages/c/srv/src/srv.c index 18b7deaed..9eba3e41e 100644 --- a/packages/c/srv/src/srv.c +++ b/packages/c/srv/src/srv.c @@ -298,6 +298,7 @@ int socket_to_socket(const srv_params_t *params, const char *auth_string, chunke res = pthread_create(&threads[1], NULL, srv_side_handle, &sides[1]); if (res != 0) { + atlogger_log(TAG, ERROR, "Failed to create thread: 1\n"); cancel_first = true; exit_res = res; goto cancel;