From c5218e41f1a8664912ec508157858883f5fac42a Mon Sep 17 00:00:00 2001 From: Ricky Zhang Date: Thu, 28 May 2026 18:34:01 -0400 Subject: [PATCH] Replace watchdog sleep with interruptable wait for faster teardown --- fuzztest/internal/BUILD | 1 + fuzztest/internal/CMakeLists.txt | 1 + fuzztest/internal/runtime.cc | 10 ++++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/fuzztest/internal/BUILD b/fuzztest/internal/BUILD index f51559105..40fdbc9d0 100644 --- a/fuzztest/internal/BUILD +++ b/fuzztest/internal/BUILD @@ -416,6 +416,7 @@ cc_library( "@abseil-cpp//absl/status:statusor", "@abseil-cpp//absl/strings", "@abseil-cpp//absl/strings:str_format", + "@abseil-cpp//absl/synchronization", "@abseil-cpp//absl/time", "@abseil-cpp//absl/types:span", "@com_google_fuzztest//common:bazel", diff --git a/fuzztest/internal/CMakeLists.txt b/fuzztest/internal/CMakeLists.txt index 0eb75aba7..3536444b1 100644 --- a/fuzztest/internal/CMakeLists.txt +++ b/fuzztest/internal/CMakeLists.txt @@ -393,6 +393,7 @@ fuzztest_cc_library( absl::statusor absl::strings absl::str_format + absl::synchronization absl::time absl::span fuzztest::bazel diff --git a/fuzztest/internal/runtime.cc b/fuzztest/internal/runtime.cc index f03dbaf18..e81a5c082 100644 --- a/fuzztest/internal/runtime.cc +++ b/fuzztest/internal/runtime.cc @@ -53,6 +53,7 @@ #include "absl/strings/str_replace.h" #include "absl/strings/string_view.h" #include "absl/strings/strip.h" +#include "absl/synchronization/notification.h" #include "absl/time/clock.h" #include "absl/time/time.h" #include "absl/types/span.h" @@ -394,14 +395,14 @@ class Runtime::Watchdog { } ~Watchdog() { - stop_requested_ = true; + stop_.Notify(); watchdog_thread_.join(); } private: void WatchdogLoop() { watchdog_thread_started_ = true; - while (!stop_requested_) { + while (!stop_.HasBeenNotified()) { runtime_.CheckWatchdogLimits(); runtime_.watchdog_spinlock_.Lock(); if (runtime_.watchdog_limit_exceeded_) { @@ -409,12 +410,13 @@ class Runtime::Watchdog { break; } runtime_.watchdog_spinlock_.Unlock(); - absl::SleepFor(absl::Seconds(1)); + // Wake on teardown via Notify(); the timeout keeps limit checks periodic. + stop_.WaitForNotificationWithTimeout(absl::Seconds(1)); } } std::atomic watchdog_thread_started_ = false; - std::atomic stop_requested_ = false; + absl::Notification stop_; std::thread watchdog_thread_; Runtime& runtime_; };