Kill the child processes at once on a second interrupt during the grace period
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use std::os::unix::process::{CommandExt, ExitStatusExt};
|
||||
use std::path::Path;
|
||||
use std::process::{Child, Stdio};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use tempfile::TempDir;
|
||||
|
||||
@@ -113,6 +115,46 @@ fn entrypoint_inherits_no_signalfd() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_interrupt_keeps_grace_period_for_child_processes() {
|
||||
let dir = TempDir::new().expect("failed to create temp dir");
|
||||
let script = "setsid -f sh -c 'trap \"\" INT; trap \"sleep 0.5; touch cleaned; exit\" TERM; touch ready; sleep 60 & wait' </dev/null >/dev/null 2>&1
|
||||
until [ -e ready ]; do sleep 0.01; done
|
||||
: > entrypoint-ready
|
||||
sleep 60";
|
||||
|
||||
let (_sandbox, mut child) = spawn_sandbox(&["--blacklist"], dir.path(), script);
|
||||
wait_for_file(&dir.path().join("entrypoint-ready"));
|
||||
send_signal(&child, libc::SIGINT);
|
||||
child.wait().unwrap();
|
||||
|
||||
assert!(
|
||||
dir.path().join("cleaned").exists(),
|
||||
"child process did not get its grace period"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interrupt_during_grace_period_kills_child_processes_at_once() {
|
||||
let dir = TempDir::new().expect("failed to create temp dir");
|
||||
let script =
|
||||
"setsid -f sh -c 'trap \"\" TERM; touch ready; sleep 60' </dev/null >/dev/null 2>&1
|
||||
until [ -e ready ]; do sleep 0.01; done";
|
||||
|
||||
let (_sandbox, mut child) = spawn_sandbox(&["--blacklist"], dir.path(), script);
|
||||
wait_for_file(&dir.path().join("ready"));
|
||||
thread::sleep(Duration::from_millis(300));
|
||||
let interrupted_at = Instant::now();
|
||||
send_signal(&child, libc::SIGINT);
|
||||
child.wait().unwrap();
|
||||
|
||||
assert!(
|
||||
interrupted_at.elapsed() < Duration::from_secs(2),
|
||||
"agent-sandbox waited {:?} after the interrupt",
|
||||
interrupted_at.elapsed()
|
||||
);
|
||||
}
|
||||
|
||||
fn spawn_sandbox(args: &[&str], dir: &Path, script: &str) -> (Sandbox, Child) {
|
||||
let mut sandbox = Sandbox::new(args);
|
||||
let child = sandbox
|
||||
|
||||
Reference in New Issue
Block a user