Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serenity = { version = "0.12", default-features = false, features = ["client", "
uuid = { version = "1", features = ["v4"] }
regex = "1"
anyhow = "1"
libc = "0.2"
rand = "0.8"
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls", "multipart", "json"] }
base64 = "0.22"
Expand Down
73 changes: 70 additions & 3 deletions src/acp/connection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::acp::protocol::{JsonRpcMessage, JsonRpcRequest, JsonRpcResponse};
#[cfg(unix)]
use libc;
use anyhow::{anyhow, Result};
use serde_json::{json, Value};
use std::collections::HashMap;
Expand Down Expand Up @@ -131,8 +133,10 @@ impl AcpConnection {
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::null())
.current_dir(working_dir)
.kill_on_drop(true);
.current_dir(working_dir);

#[cfg(unix)]
cmd.process_group(0);
for (k, v) in env {
cmd.env(k, expand_env(v));
}
Expand Down Expand Up @@ -384,10 +388,25 @@ impl AcpConnection {
}
}

#[cfg(unix)]
impl Drop for AcpConnection {
fn drop(&mut self) {
if let Some(pid) = self._proc.id() {
// Send SIGTERM to the entire process group (-PGID) to clean up orphaned grandchildren
unsafe {
let pgid = pid as i32;
libc::kill(-pgid, libc::SIGTERM);
}
}
}
}

#[cfg(test)]
mod tests {
use super::{build_permission_response, pick_best_option};
use super::{build_permission_response, pick_best_option, AcpConnection};
use serde_json::json;
use std::collections::HashMap;
use tokio::time::Duration;

#[test]
fn picks_allow_always_over_other_options() {
Expand Down Expand Up @@ -479,4 +498,52 @@ mod tests {
json!({"outcome": {"outcome": "selected", "optionId": "allow_always"}})
);
}

#[tokio::test]
async fn test_process_group_cleanup() -> anyhow::Result<()> {
#[cfg(unix)]
{
// A script that spawns a background process and stays alive
// We use 'sleep 100' as a grandchild that should be killed
let script = "sh -c 'sleep 100' & sleep 100";

let conn =
AcpConnection::spawn("sh", &["-c".to_string(), script.to_string()], ".", &HashMap::new()).await?;

tokio::time::sleep(Duration::from_millis(500)).await;

let pid = conn._proc.id().expect("should have pid");

// Find grandchild pid
let output = std::process::Command::new("pgrep")
.arg("-P")
.arg(pid.to_string())
.output()?;
let grandchild_pid_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
assert!(
!grandchild_pid_str.is_empty(),
"Grandchild process should exist"
);
// If multiple, take the first one
let grandchild_pid_str = grandchild_pid_str.lines().next().unwrap();
let grandchild_pid: i32 = grandchild_pid_str.parse().expect("should be a pid");

// Drop the connection, which should kill the group
drop(conn);

tokio::time::sleep(Duration::from_millis(500)).await;

// Check if grandchild is gone. kill -0 pid checks if process exists.
let status = std::process::Command::new("kill")
.arg("-0")
.arg(grandchild_pid.to_string())
.status();

assert!(
status.is_err() || !status.unwrap().success(),
"Grandchild process should be killed"
);
}
Ok(())
}
}