Skip to content

sandbox: fix race condition#12

Open
aokblast wants to merge 7 commits into
masterfrom
sandbox/fix_race
Open

sandbox: fix race condition#12
aokblast wants to merge 7 commits into
masterfrom
sandbox/fix_race

Conversation

@aokblast

Copy link
Copy Markdown
Contributor

The waiting thread set a global variable to indicate if the child process is killed by timeout. We use a mutex to wrap it so that we can get rid of race condition.

The waiting thread set a global variable to indicate if the child
process is killed by timeout. We use a mutex to wrap it so that we can
get rid of race condition.
@uier uier requested a review from Copilot October 25, 2025 15:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a race condition in the sandbox timeout mechanism by adding proper mutex synchronization around the g_time_limit_exceeded_killed flag. The main thread now safely reads the timeout status before checking exit conditions.

Key changes:

  • Added mutex protection for the shared g_time_limit_exceeded_killed variable
  • Introduced a local copy of the timeout flag to avoid repeated mutex operations
  • Made the watcher thread ID global to enable proper thread cancellation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sandbox.c Outdated
Comment thread sandbox.c
Comment thread sandbox.c Outdated
Eliminate the global pthread mutex and related lock/unlock calls. Read
g_time_limit_exceeded_killed after pthread_join to avoid races instead
of synchronizing with a mutex and simplify the watcher thread.
@as535364 as535364 changed the base branch from globalization_static to master October 28, 2025 12:10
@as535364 as535364 requested a review from Copilot October 28, 2025 17:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sandbox.c Outdated
Comment thread sandbox.c Outdated
Comment thread sandbox.c
@as535364 as535364 requested a review from Copilot October 31, 2025 07:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sandbox.c
#endif

pthread_mutex_lock(&g_mtx);
g_pid = fork();

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locking a mutex before fork() without unlocking it in the child process creates a deadlock risk. After fork(), the child process inherits the locked mutex but the owning thread doesn't exist in the child. The mutex at line 310 will deadlock. The parent unlocks at line 132, but the child never unlocks before attempting to lock again at line 310.

Suggested change
g_pid = fork();
g_pid = fork();
if (g_pid == 0)
{
// Child process
// Reinitialize the mutex to avoid deadlock
pthread_mutex_init(&g_mtx, NULL);
}

Copilot uses AI. Check for mistakes.
Comment thread sandbox.c
Comment on lines +310 to 311
pthread_mutex_lock(&g_mtx);

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locking the mutex immediately before execvp() serves no purpose since execvp() replaces the entire process image. The mutex state becomes irrelevant after exec. Additionally, this will deadlock because the mutex was already locked by the parent at line 111 and never unlocked in the child process.

Suggested change
pthread_mutex_lock(&g_mtx);

Copilot uses AI. Check for mistakes.
Comment thread sandbox.c
pthread_create(&thread_id, NULL, &watcher_thread, NULL);
if (time_limit) {
if (pthread_create(&g_thread_id, NULL, &watcher_thread, NULL) != 0) {
kill(g_pid, SIGKILL);

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After killing the child process with SIGKILL, the parent should call waitpid() or wait() to reap the child and prevent it from becoming a zombie process.

Suggested change
kill(g_pid, SIGKILL);
kill(g_pid, SIGKILL);
waitpid(g_pid, NULL, 0); // Reap the child to prevent zombie

Copilot uses AI. Check for mistakes.
Comment thread sandbox.c
Comment on lines +143 to +144
pthread_cancel(g_thread_id);
pthread_join(g_thread_id, NULL);

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using pthread_cancel() on a thread that calls non-cancellation-safe functions (usleep, kill) can lead to undefined behavior. The watcher thread may be cancelled while holding internal locks or in the middle of system calls. Consider using a flag-based approach or ensuring the thread has proper cancellation points.

Copilot uses AI. Check for mistakes.
Comment thread sandbox.c
return 1;
}
}
pthread_mutex_unlock(&g_mtx);

Copilot AI Oct 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If pthread_create() fails at line 125, execution returns at line 129 without unlocking the mutex that was locked at line 111. This creates a resource leak where the mutex remains locked.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants