mirror of
https://github.com/jparkerweb/plan2code.git
synced 2026-12-05 08:15:41 -08:00
End the agent's process group and the consoles it opened on every quit
AI Assisted
This commit is contained in:
1 parent
98bdbc97c3
commit
6a93395148
8 files changed
+1363
-58
No files matched your search
@@ -103,6 +103,102 @@ fn kill_ends_a_running_step() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A grandchild that ignores hangup is still in the agent's process group
|
||||
/// (portable-pty starts the agent under `setsid`), so `kill` must reach it
|
||||
/// through the group once the grace is up. An ordinary grandchild already dies
|
||||
/// with its shell, when the kernel hangs up the terminal, so it cannot show this.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn kill_ends_a_hangup_ignoring_grandchild_after_the_grace() {
|
||||
use plan2code_app_lib::pty_host::HANGUP_GRACE;
|
||||
|
||||
let (host, pid) =
|
||||
start_sh_with_grandchild("trap '' HUP; sleep 300 & echo GRANDCHILD=$! END; wait");
|
||||
assert!(alive(pid), "the grandchild should be running before kill");
|
||||
|
||||
host.kill().unwrap();
|
||||
host.wait().unwrap();
|
||||
assert!(
|
||||
gone_within(pid, HANGUP_GRACE + Duration::from_secs(2)),
|
||||
"the hangup-ignoring grandchild outlived kill()"
|
||||
);
|
||||
}
|
||||
|
||||
/// An agent that ended by itself was never killed, so no grace thread of
|
||||
/// `kill`'s is there to finish its group: only `end_group` can end the
|
||||
/// grandchild that ignored the terminal's hangup.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn end_group_ends_what_a_command_that_exited_by_itself_left() {
|
||||
let (host, pid) = start_sh_with_grandchild(
|
||||
"(trap '' HUP; exec sleep 300) & echo GRANDCHILD=$! END; sleep 0.2; exit 0",
|
||||
);
|
||||
host.wait().unwrap();
|
||||
assert!(alive(pid), "the grandchild outlived its shell, as set up");
|
||||
|
||||
host.end_group();
|
||||
// Only the moment it takes the reaper to collect it.
|
||||
assert!(
|
||||
gone_within(pid, Duration::from_secs(1)),
|
||||
"the grandchild outlived end_group()"
|
||||
);
|
||||
}
|
||||
|
||||
/// `script` under `/bin/sh -c` on a pty; it must print `GRANDCHILD=<pid> END`.
|
||||
/// Returns the host and that pid.
|
||||
#[cfg(unix)]
|
||||
fn start_sh_with_grandchild(script: &str) -> (PtyHost, i32) {
|
||||
let (tx, rx) = channel();
|
||||
let host = PtyHost::start(
|
||||
StartOptions {
|
||||
program: "/bin/sh".to_string(),
|
||||
args: vec!["-c".to_string(), script.to_string()],
|
||||
env: vec![],
|
||||
cwd: None,
|
||||
cols: 80,
|
||||
rows: 24,
|
||||
},
|
||||
move |bytes| {
|
||||
let _ = tx.send(bytes);
|
||||
},
|
||||
)
|
||||
.expect("start the shell");
|
||||
let mut seen = String::new();
|
||||
wait_for(&host, &rx, &mut seen, " END");
|
||||
let pid = seen
|
||||
.rsplit("GRANDCHILD=")
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(|c: char| !c.is_ascii_digit())
|
||||
.next()
|
||||
.unwrap()
|
||||
.parse()
|
||||
.expect("the grandchild's pid");
|
||||
(host, pid)
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn alive(pid: i32) -> bool {
|
||||
// SAFETY: signal 0 only asks whether the process exists.
|
||||
unsafe { libc::kill(pid, 0) == 0 }
|
||||
}
|
||||
|
||||
/// Whether `pid` is gone within `within`; killed on the way out either way,
|
||||
/// so a failing test leaves nothing behind.
|
||||
#[cfg(unix)]
|
||||
fn gone_within(pid: i32, within: Duration) -> bool {
|
||||
let deadline = Instant::now() + within;
|
||||
while alive(pid) && Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
let gone = !alive(pid);
|
||||
if !gone {
|
||||
// SAFETY: a process this test started.
|
||||
unsafe { libc::kill(pid, libc::SIGKILL) };
|
||||
}
|
||||
gone
|
||||
}
|
||||
|
||||
/// An npm `.cmd` shim: `cmd.exe` is the directly spawned process, the real
|
||||
/// program (here, the fake agent) its grandchild. `kill` must end both, not
|
||||
/// just `cmd.exe` (Task 9.8).
|
||||
|
||||
Reference in new issue
Block a user