Skip to content

Commit

Permalink
Merge pull request #622 from coder-y04/deal_zombie_processes
Browse files Browse the repository at this point in the history
Dealing with optimizer-server zombie processes
  • Loading branch information
imeoer authored Dec 4, 2024
2 parents 5f948e4 + 917375e commit 021c505
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pkg/fanotify/fanotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ func (fserver *Server) RunServer() error {
}
fserver.Cmd = cmd

go func() {
if err := cmd.Wait(); err != nil {
logrus.WithError(err).Errorf("Failed to wait for fserver to finish")
}
}()

go func() {
if err := fserver.RunReceiver(); err != nil {
logrus.WithError(err).Errorf("Failed to receive event information from server")
Expand Down
32 changes: 23 additions & 9 deletions tools/optimizer-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ use std::{
use nix::{
poll::{poll, PollFd, PollFlags},
sched::{setns, CloneFlags},
unistd::{
fork, getpgid,
ForkResult::{Child, Parent},
},
sys::wait::{waitpid, WaitStatus},
unistd::{fork, getpgid, ForkResult},
};
use serde::Serialize;

Expand Down Expand Up @@ -259,19 +257,35 @@ fn main() {
return;
}
}
let pid = unsafe { fork() };
match pid.expect("fork failed: unable to create child process") {
Child => {

match unsafe { fork() } {
Ok(ForkResult::Child) => {
if let Err(e) = start_fanotify() {
eprintln!("failed to start fanotify server {e:?}");
}
}
Parent { child } => {
Ok(ForkResult::Parent { child }) => {
if let Err(e) = getpgid(Some(child)).map(|pgid| {
eprintln!("forked optimizer server subprocess, pid: {child}, pgid: {pgid}");
}) {
eprintln!("failed to get pgid of {child} {e:?}");
};
}

match waitpid(child, None) {
Ok(WaitStatus::Signaled(pid, signal, _)) => {
eprintln!("child process {pid} was killed by signal {signal}");
}
Ok(WaitStatus::Stopped(pid, signal)) => {
eprintln!("child process {pid} was stopped by signal {signal}");
}
Err(e) => {
eprintln!("failed to wait for child process: {e}");
}
_ => {}
}
}
Err(e) => {
eprintln!("fork failed: unable to create child process: {e:?}");
}
}
}

0 comments on commit 021c505

Please sign in to comment.