Optional signal value for kill on timeouts in TimeoutForkserverExecutor (#461)

* Optional signal value to kill forked processes on timeout

* Cargo format

* Properly initialize TimeoutForkserverExecutor

* Added with_signal constructor

* Removed duplicate code
This commit is contained in:
buherator 2022-01-09 14:31:14 +01:00 committed by GitHub
parent e6f2f2d0b2
commit 5ac3cd6b5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -378,14 +378,25 @@ pub trait HasForkserver {
pub struct TimeoutForkserverExecutor<E: Debug> { pub struct TimeoutForkserverExecutor<E: Debug> {
executor: E, executor: E,
timeout: TimeSpec, timeout: TimeSpec,
signal: Signal,
} }
impl<E: Debug> TimeoutForkserverExecutor<E> { impl<E: Debug> TimeoutForkserverExecutor<E> {
/// Create a new [`TimeoutForkserverExecutor`] /// Create a new [`TimeoutForkserverExecutor`]
pub fn new(executor: E, exec_tmout: Duration) -> Result<Self, Error> { pub fn new(executor: E, exec_tmout: Duration) -> Result<Self, Error> {
let signal = Signal::SIGKILL;
Self::with_signal(executor, exec_tmout, signal)
}
/// Create a new [`TimeoutForkserverExecutor`] that sends a user-defined signal to the timed-out process
pub fn with_signal(executor: E, exec_tmout: Duration, signal: Signal) -> Result<Self, Error> {
let milli_sec = exec_tmout.as_millis() as i64; let milli_sec = exec_tmout.as_millis() as i64;
let timeout = TimeSpec::milliseconds(milli_sec); let timeout = TimeSpec::milliseconds(milli_sec);
Ok(Self { executor, timeout }) Ok(Self {
executor,
timeout,
signal,
})
} }
} }
@ -466,7 +477,7 @@ where
self.executor.forkserver_mut().set_last_run_timed_out(1); self.executor.forkserver_mut().set_last_run_timed_out(1);
// We need to kill the child in case he has timed out, or we can't get the correct pid in the next call to self.executor.forkserver_mut().read_st()? // We need to kill the child in case he has timed out, or we can't get the correct pid in the next call to self.executor.forkserver_mut().read_st()?
let _ = kill(self.executor.forkserver().child_pid(), Signal::SIGKILL); let _ = kill(self.executor.forkserver().child_pid(), self.signal);
let (recv_status_len, _) = self.executor.forkserver_mut().read_st()?; let (recv_status_len, _) = self.executor.forkserver_mut().read_st()?;
if recv_status_len != 4 { if recv_status_len != 4 {
return Err(Error::Forkserver( return Err(Error::Forkserver(