Fix Rust formatting and clippy warnings

- Fix cargo fmt formatting issues
- Fix unused variable warnings by prefixing with underscore
- Fix if_not_else clippy warning
- Fix uninlined_format_args warnings
- Fix redundant closure warning
This commit is contained in:
Peter Steinberger 2025-06-18 15:24:17 +02:00
parent e3e92c646d
commit 4a0121eabe
3 changed files with 43 additions and 35 deletions

View file

@ -1047,14 +1047,17 @@ fn handle_session_kill(control_path: &Path, path: &str) -> Response<String> {
if let Ok(session_info) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(pid) = session_info.get("pid").and_then(serde_json::Value::as_u64) {
// Wait for the process to actually die
for _ in 0..30 { // 30 * 100ms = 3 seconds max
for _ in 0..30 {
// 30 * 100ms = 3 seconds max
// Only reap zombies for PTY sessions
if let Some(spawn_type) = session_info.get("spawn_type").and_then(|s| s.as_str()) {
if let Some(spawn_type) =
session_info.get("spawn_type").and_then(|s| s.as_str())
{
if spawn_type == "pty" {
sessions::reap_zombies();
}
}
if !sessions::is_pid_alive(pid as u32) {
process_died = true;
break;

View file

@ -256,14 +256,14 @@ pub fn is_pid_alive(pid: u32) -> bool {
match output {
Ok(output) => {
if !output.status.success() {
// Process doesn't exist
false
} else {
if output.status.success() {
// Check if it's a zombie process (status starts with 'Z')
let stat = String::from_utf8_lossy(&output.stdout);
let stat = stat.trim();
!stat.starts_with('Z')
} else {
// Process doesn't exist
false
}
}
Err(_) => false,
@ -272,20 +272,20 @@ pub fn is_pid_alive(pid: u32) -> bool {
/// Attempt to reap zombie children
pub fn reap_zombies() {
use std::ptr;
use libc::{waitpid, WNOHANG, WUNTRACED};
use std::ptr;
loop {
// Try to reap any zombie children
let result = unsafe { waitpid(-1, ptr::null_mut(), WNOHANG | WUNTRACED) };
if result <= 0 {
// No more children to reap or error occurred
break;
}
// Successfully reaped a zombie child
eprintln!("Reaped zombie child with PID: {}", result);
eprintln!("Reaped zombie child with PID: {result}");
}
}

View file

@ -103,7 +103,6 @@ fn spawn_via_socket_impl(command: &[String], working_dir: Option<&str>) -> Resul
fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String> {
let session_id = Uuid::new_v4().to_string();
// Create PTY
let pty_result = openpty(
&Winsize {
@ -218,26 +217,26 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
ForkResult::Child => {
// Child process - set up PTY and exec command
if let Err(e) = close(master_fd) {
if let Err(_e) = close(master_fd) {
std::process::exit(1);
}
// Create new session
if let Err(e) = setsid() {
if let Err(_e) = setsid() {
std::process::exit(1);
}
// Set up stdin/stdout/stderr to use the slave PTY
if let Err(e) = dup2(slave_fd, 0) {
if let Err(_e) = dup2(slave_fd, 0) {
std::process::exit(1);
}
if let Err(e) = dup2(slave_fd, 1) {
if let Err(_e) = dup2(slave_fd, 1) {
std::process::exit(1);
}
if let Err(e) = dup2(slave_fd, 2) {
if let Err(_e) = dup2(slave_fd, 2) {
std::process::exit(1);
}
if let Err(e) = close(slave_fd) {
if let Err(_e) = close(slave_fd) {
std::process::exit(1);
}
@ -255,7 +254,7 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
dir.to_string()
};
if let Err(e) = std::env::set_current_dir(&expanded_dir) {
if let Err(_e) = std::env::set_current_dir(&expanded_dir) {
std::process::exit(1);
}
}
@ -276,7 +275,6 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
command.first().unwrap()
};
let args: Vec<CString> = if command.is_empty() {
vec![CString::new(program)?]
} else {
@ -287,12 +285,10 @@ fn spawn_via_pty(command: &[String], working_dir: Option<&str>) -> Result<String
.map_err(|e| anyhow::anyhow!("Invalid command argument: {}", e))?
};
// Use execvp to execute the command
match nix::unistd::execvp(&CString::new(program)?, &args) {
Ok(_) => {
}
Err(e) => {
Ok(_) => {}
Err(_e) => {
std::process::exit(127); // Standard exit code for command not found
}
}
@ -348,7 +344,9 @@ fn handle_pty_session(
let stdin_path_clone = stdin_path;
let session_id_clone = session_id.to_string();
std::thread::spawn(move || {
if let Err(e) = handle_stdin_to_pty(master_fd_dup2, &stdin_path_clone, &session_id_clone) {
if let Err(e) =
handle_stdin_to_pty(master_fd_dup2, &stdin_path_clone, &session_id_clone)
{
eprintln!("Stdin handler error: {e}");
}
// Clean up the duplicated fd when done
@ -451,26 +449,33 @@ fn handle_stdin_to_pty(master_fd: RawFd, stdin_path: &str, session_id: &str) ->
// Check for Ctrl+C and send SIGINT directly for responsiveness
if n == 1 && buffer[0] == 0x03 {
// Ctrl+C detected - send SIGINT to process group for immediate response
let session_json_path = format!("{}/{}/session.json",
let session_json_path = format!(
"{}/{}/session.json",
env::var("TTY_FWD_CONTROL_DIR").unwrap_or_else(|_| {
format!("{}/.vibetunnel/control", env::var("HOME").unwrap_or_default())
}),
session_id);
format!(
"{}/.vibetunnel/control",
env::var("HOME").unwrap_or_default()
)
}),
session_id
);
if let Ok(content) = std::fs::read_to_string(&session_json_path) {
if let Ok(session_info) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(pid) = session_info.get("pid").and_then(|p| p.as_u64()) {
if let Ok(session_info) =
serde_json::from_str::<serde_json::Value>(&content)
{
if let Some(pid) = session_info.get("pid").and_then(serde_json::Value::as_u64) {
// Send SIGINT to the process group for immediate response
unsafe {
libc::kill(-(pid as i32), libc::SIGINT);
}
eprintln!("Sent SIGINT to process group {}", pid);
eprintln!("Sent SIGINT to process group {pid}");
}
}
}
// Still write Ctrl+C through PTY for terminal consistency
}
// Write to PTY master using libc::write (blocking)
let bytes_written =
unsafe { libc::write(master_fd, buffer.as_ptr().cast::<libc::c_void>(), n) };