diff --git a/sandboxed_api/sandbox2/CMakeLists.txt b/sandboxed_api/sandbox2/CMakeLists.txt index 27bc89b..984c23e 100644 --- a/sandboxed_api/sandbox2/CMakeLists.txt +++ b/sandboxed_api/sandbox2/CMakeLists.txt @@ -301,11 +301,11 @@ target_link_libraries(sandbox2_sandbox2 absl::optional absl::str_format absl::strings - absl::synchronization sapi::strerror sapi::base PUBLIC absl::status absl::statusor + absl::synchronization absl::time sapi::config sapi::file_base diff --git a/sandboxed_api/sandbox2/sandbox2.cc b/sandboxed_api/sandbox2/sandbox2.cc index ebbd9ab..d57e727 100644 --- a/sandboxed_api/sandbox2/sandbox2.cc +++ b/sandboxed_api/sandbox2/sandbox2.cc @@ -22,6 +22,7 @@ #include "absl/memory/memory.h" #include "absl/status/statusor.h" +#include "absl/synchronization/mutex.h" #include "absl/time/time.h" #include "sandboxed_api/sandbox2/monitor.h" #include "sandboxed_api/sandbox2/result.h" @@ -44,14 +45,17 @@ absl::StatusOr Sandbox2::AwaitResultWithTimeout( if (!done) { return absl::DeadlineExceededError("Sandbox did not finish within timeout"); } - monitor_thread_->join(); + { + absl::MutexLock lock(&monitor_notify_mutex_); + monitor_thread_->join(); - CHECK(IsTerminated()) << "Monitor did not terminate"; + CHECK(IsTerminated()) << "Monitor did not terminate"; - // Reset the Monitor Thread object to its initial state, as to mark that this - // object cannot be used anymore to control behavior of the sandboxee (e.g. - // via signals). - monitor_thread_.reset(nullptr); + // Reset the Monitor Thread object to its initial state, as to mark that + // this object cannot be used anymore to control behavior of the sandboxee + // (e.g. via signals). + monitor_thread_.reset(); + } VLOG(1) << "Final execution status: " << monitor_->result_.ToString(); CHECK(monitor_->result_.final_status() != Result::UNSET); @@ -74,6 +78,7 @@ bool Sandbox2::RunAsync() { } void Sandbox2::NotifyMonitor() { + absl::ReaderMutexLock lock(&monitor_notify_mutex_); if (monitor_thread_ != nullptr) { pthread_kill(monitor_thread_->native_handle(), SIGCHLD); } diff --git a/sandboxed_api/sandbox2/sandbox2.h b/sandboxed_api/sandbox2/sandbox2.h index 6ec1db1..5f55530 100644 --- a/sandboxed_api/sandbox2/sandbox2.h +++ b/sandboxed_api/sandbox2/sandbox2.h @@ -27,6 +27,7 @@ #include "absl/base/macros.h" #include "absl/memory/memory.h" #include "absl/status/statusor.h" +#include "absl/synchronization/mutex.h" #include "sandboxed_api/sandbox2/comms.h" #include "sandboxed_api/sandbox2/executor.h" #include "sandboxed_api/sandbox2/ipc.h" @@ -136,6 +137,9 @@ class Sandbox2 final { // Monitor thread object - owned by Sandbox2. std::unique_ptr monitor_thread_; + + // Synchronizes monitor thread deletion and notifying the monitor. + absl::Mutex monitor_notify_mutex_; }; } // namespace sandbox2