Skip to content

Commit

Permalink
Merge pull request #1526 from atsign-foundation/c-srv-pointer-conv
Browse files Browse the repository at this point in the history
fix: tid set to null
  • Loading branch information
XavierChanth authored Nov 12, 2024
2 parents 4cb9ac0 + cd20138 commit 2b81478
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/c/srv/src/srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -299,6 +299,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;
}
Expand All @@ -316,17 +317,21 @@ 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;
} else {
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);
Expand Down

0 comments on commit 2b81478

Please sign in to comment.