Skip to content

Commit

Permalink
server: bugfix: handle potential thread leak on spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Feb 20, 2022
1 parent 63aa13d commit 24cc0a9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/rpcserver/rpcserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ void wait_process_exit_thread(wait_process_exit_thread_t *params)

bool handle_exec(int sockfd)
{
pthread_t thread = 0;
pid_t pid = INVALID_PID;
int master = -1;
int result = false;
Expand Down Expand Up @@ -288,8 +289,7 @@ bool handle_exec(int sockfd)
thread_params->pid = pid;
CHECK(0 == pipe(thread_params->pipe));

pthread_t thread;
CHECK(0 == pthread_create(&thread, NULL, wait_process_exit_thread, thread_params));
CHECK(0 == pthread_create(&thread, NULL, (void * (*)(void *))wait_process_exit_thread, thread_params));

TRACE("wait for thread to reach waitpid");

Expand Down Expand Up @@ -318,9 +318,12 @@ bool handle_exec(int sockfd)
nbytes = read(master, buf, BUFFERSIZE);
if (nbytes < 1)
{
TRACE("read master failed. break");
break;
}

TRACE("master->sock");

cmd_exec_chunk_t chunk;
chunk.type = CMD_EXEC_CHUNK_TYPE_STDOUT;
chunk.size = nbytes;
Expand All @@ -336,6 +339,9 @@ bool handle_exec(int sockfd)
{
break;
}

TRACE("sock->master");

CHECK(writeall(master, buf, nbytes));
}
}
Expand All @@ -344,13 +350,18 @@ bool handle_exec(int sockfd)
CHECK(sizeof(byte) == write(thread_params->pipe[1], &byte, sizeof(byte)));

TRACE("wait for thread to finish");
CHECK(0 != pthread_join(&thread, NULL));
CHECK(0 != pthread_join(thread, NULL));

TRACE("thread exit");

result = true;

error:
if (thread)
{
pthread_kill(thread, SIGKILL);
}

if (INVALID_PID == pid)
{
// failed to create process somewhere in the prolog, at least notify
Expand Down

0 comments on commit 24cc0a9

Please sign in to comment.