diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index d0a2763..9bb8d24 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -440,8 +440,19 @@ fn run_daemon(id: &str, command: &[String], root_dir: &Path) -> Result<()> { | |||
| 440 | let pid_file_data = PidFile::new(child.id(), command.to_vec()); | 440 | let pid_file_data = PidFile::new(child.id(), command.to_vec()); |
| 441 | pid_file_data.write_to_file(&pid_file)?; | 441 | pid_file_data.write_to_file(&pid_file)?; |
| 442 | 442 | ||
| 443 | // Don't wait for the child - let it run detached | 443 | // Properly detach the child process |
| 444 | std::mem::forget(child); | 444 | // Instead of using std::mem::forget which prevents proper cleanup, |
| 445 | // we spawn a background thread to handle the child process lifecycle | ||
| 446 | std::thread::spawn(move || { | ||
| 447 | // The child process is moved into this thread | ||
| 448 | // When this thread ends, the Child's Drop implementation will run | ||
| 449 | // This ensures proper resource cleanup while still detaching the process | ||
| 450 | |||
| 451 | // We don't need to wait for the child since we want it to run independently | ||
| 452 | // But by letting the Child's Drop trait run, we ensure proper cleanup | ||
| 453 | // The process will become the child of init (PID 1) which will reap it | ||
| 454 | drop(child); | ||
| 455 | }); | ||
| 445 | 456 | ||
| 446 | println!( | 457 | println!( |
| 447 | "Started daemon '{}' with PID written to {}", | 458 | "Started daemon '{}' with PID written to {}", |
