Skip to content

Commit d1e8fbb

Browse files
FuzzTest Teamcopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 900320823
1 parent f959b01 commit d1e8fbb

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

centipede/rusage_profiler.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ const RUsageProfiler::Snapshot& RUsageProfiler::TakeSnapshot( //
372372
return kEmpty;
373373
}
374374

375-
absl::WriterMutexLock lock{mutex_};
375+
absl::WriterMutexLock lock{snapshots_mutex_};
376376

377377
RUsageTiming snap_timing = RUsageTiming::Zero();
378378
RUsageTiming delta_timing = RUsageTiming::Zero();
@@ -420,7 +420,7 @@ void RUsageProfiler::StartTimelapse( //
420420
absl::Duration interval, //
421421
bool also_log, //
422422
std::string title) {
423-
absl::WriterMutexLock lock{mutex_};
423+
absl::WriterMutexLock lock{recorder_mutex_};
424424
FUZZTEST_CHECK(!timelapse_recorder_) << "StopTimelapse() wasn't called";
425425
timelapse_recorder_ = std::make_unique<PeriodicAction>(
426426
[this, loc = std::move(loc), title = std::move(title), also_log]() {
@@ -431,7 +431,7 @@ void RUsageProfiler::StartTimelapse( //
431431
}
432432

433433
void RUsageProfiler::StopTimelapse() {
434-
absl::WriterMutexLock lock{mutex_};
434+
absl::WriterMutexLock lock{recorder_mutex_};
435435
FUZZTEST_CHECK(timelapse_recorder_) << "StartTimelapse() wasn't called";
436436
timelapse_recorder_.reset();
437437
}
@@ -483,7 +483,7 @@ void RUsageProfiler::PrintReport( //
483483

484484
void RUsageProfiler::GenerateReport(
485485
ReportSink* absl_nonnull report_sink) const {
486-
absl::ReaderMutexLock lock{mutex_};
486+
absl::ReaderMutexLock lock{snapshots_mutex_};
487487
// Prevent interleaved reports from multiple concurrent RUsageProfilers.
488488
ABSL_CONST_INIT static absl::Mutex report_generation_mutex_{absl::kConstInit};
489489
absl::WriterMutexLock logging_lock{report_generation_mutex_};

centipede/rusage_profiler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,17 +427,19 @@ class RUsageProfiler {
427427
// The sequential ID of this profiler. Used to annotate all log
428428
const int id_;
429429

430-
// Mutex for the mutable data further below.
431-
mutable absl::Mutex mutex_;
430+
// Mutexes for the mutable data further below.
431+
mutable absl::Mutex snapshots_mutex_;
432+
mutable absl::Mutex recorder_mutex_;
432433

433434
// Chronological snapshots. Using std::deque gives a better-than-vector
434435
// average insertion speed, preserves iterators across insertions, and strikes
435436
// a balance between vector's and list's additional storage.
436-
std::deque<Snapshot> snapshots_ ABSL_GUARDED_BY(mutex_);
437+
std::deque<Snapshot> snapshots_ ABSL_GUARDED_BY(snapshots_mutex_);
437438
// A temporarily lived periodic action that records and optionally logs
438439
// timelapse snapshots. (Re)created by each new call to StartTimelapse() and
439440
// terminated by StopTimelapse() or the dtor, whichever comes first.
440-
std::unique_ptr<PeriodicAction> timelapse_recorder_ ABSL_GUARDED_BY(mutex_);
441+
std::unique_ptr<PeriodicAction> timelapse_recorder_
442+
ABSL_GUARDED_BY(recorder_mutex_);
441443

442444
// An auto-starting timer passed to RUsageTiming::Snapshot() in order to track
443445
// this RUsageProfiler object's lifetime stats rather than the process's

centipede/rusage_profiler_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,24 @@ TEST(RUsageProfilerTest, ValidateReport) {
245245
rprof.GenerateReport(&report_capture);
246246
}
247247

248+
TEST(RUsageProfilerTest, DeadlockReproduction) {
249+
const auto rusage_scope = RUsageScope::ThisProcess();
250+
for (int i = 0; i < 1000; ++i) {
251+
RUsageProfiler rprof{rusage_scope,
252+
RUsageProfiler::kAllMetrics,
253+
RUsageProfiler::kRaiiOff,
254+
{__FILE__, __LINE__}};
255+
// Use a tiny interval to trigger frequent snapshots
256+
rprof.StartTimelapse({__FILE__, __LINE__}, absl::Microseconds(10),
257+
/*also_log=*/false, "Timelapse");
258+
259+
// Briefly wait to ensure the background thread starts running
260+
absl::SleepFor(absl::Microseconds(10));
261+
262+
// This call frequently deadlocks because it holds the mutex while
263+
// waiting for the background thread to finish its TakeSnapshot() call.
264+
rprof.StopTimelapse();
265+
}
266+
}
267+
248268
} // namespace fuzztest::internal

0 commit comments

Comments
 (0)