Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Produce an error if coverage recording failed due to timeout (#2529)
Browse files Browse the repository at this point in the history
Closes #2520.

After investigation the underlying cause here is that the process is getting killed due to timeout, but we don't report the timeout or produce an error. Modify the coverage code so that it fails if timeout is hit.
  • Loading branch information
Porges authored Oct 18, 2022
1 parent b3fd6d5 commit c46c6be
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/agent/coverage/src/block/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::collections::BTreeMap;
use std::convert::TryInto;
use std::ffi::OsStr;
use std::process::Command;
use std::sync::mpsc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -40,7 +41,7 @@ impl<'c> Recorder<'c> {
let mut tracer = Ptracer::new();
let mut child = tracer.spawn(cmd)?;

let _timer = Timer::new(timeout, move || child.kill());
let timer = Timer::new(timeout, move || child.kill());

let recorder = Recorder {
breakpoints: Breakpoints::default(),
Expand All @@ -54,7 +55,14 @@ impl<'c> Recorder<'c> {

let coverage = recorder.wait()?;

Ok(coverage)
if timer.timed_out() {
Err(anyhow::format_err!(
"timed out creating recording after {}s",
timeout.as_secs_f64()
))
} else {
Ok(coverage)
}
}

fn wait(mut self) -> Result<CommandBlockCov> {
Expand Down Expand Up @@ -429,6 +437,7 @@ const MAX_POLL_PERIOD: Duration = Duration::from_millis(500);

pub struct Timer {
sender: mpsc::Sender<()>,
timed_out: Arc<AtomicBool>,
_handle: thread::JoinHandle<()>,
}

Expand All @@ -438,7 +447,9 @@ impl Timer {
F: FnOnce() -> T + Send + 'static,
{
let (sender, receiver) = std::sync::mpsc::channel();
let timed_out = Arc::new(AtomicBool::new(false));

let set_timed_out = timed_out.clone();
let _handle = thread::spawn(move || {
let poll_period = Duration::min(timeout, MAX_POLL_PERIOD);
let start = Instant::now();
Expand All @@ -455,11 +466,20 @@ impl Timer {
}
}

set_timed_out.store(true, Ordering::SeqCst);
// Timed out, so call back.
on_timeout();
});

Self { sender, _handle }
Self {
sender,
_handle,
timed_out,
}
}

pub fn timed_out(&self) -> bool {
self.timed_out.load(Ordering::SeqCst)
}

pub fn cancel(self) {
Expand Down

0 comments on commit c46c6be

Please sign in to comment.