Skip to content

Commit

Permalink
Fix a data race
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 705003451
Change-Id: I15dcab987592a1029c57fbcde687932bdea4d417
  • Loading branch information
happyCoder92 authored and copybara-github committed Dec 11, 2024
1 parent f06ee44 commit 037c5c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion sandboxed_api/sandbox2/monitor_ptrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ void PtraceMonitor::NotifyMonitor() {
}

void PtraceMonitor::Join() {
absl::MutexLock lock(&thread_mutex_);
if (thread_.IsJoinable()) {
thread_.Join();
CHECK(IsDone()) << "Monitor did not terminate";
Expand All @@ -215,7 +216,10 @@ void PtraceMonitor::Join() {
}

void PtraceMonitor::RunInternal() {
thread_ = sapi::Thread(this, &PtraceMonitor::Run, "sandbox2-Monitor");
{
absl::MutexLock lock(&thread_mutex_);
thread_ = sapi::Thread(this, &PtraceMonitor::Run, "sandbox2-Monitor");
}

// Wait for the Monitor to set-up the sandboxee correctly (or fail while
// doing that). From here on, it is safe to use the IPC object for
Expand Down
4 changes: 3 additions & 1 deletion sandboxed_api/sandbox2/monitor_ptrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ class PtraceMonitor : public MonitorBase {
// PidWaiter for waiting for sandboxee events.
PidWaiter pid_waiter_;

// Synchronizes joining the monitor thread.
absl::Mutex thread_mutex_;
// Monitor thread object.
sapi::Thread thread_;
sapi::Thread ABSL_GUARDED_BY(thread_mutex_) thread_;

// Synchronizes deadline setting and notifying the monitor.
absl::Mutex notify_mutex_;
Expand Down

0 comments on commit 037c5c2

Please sign in to comment.