Skip to content

Commit

Permalink
fix process exit (not init) event not publish
Browse files Browse the repository at this point in the history
1.add get id interface in process
2.add process event publish in service

Signed-off-by: jokemanfire <[email protected]>
  • Loading branch information
jokemanfire authored and mxpv committed Dec 11, 2024
1 parent 1e17e14 commit eef8806
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
5 changes: 5 additions & 0 deletions crates/runc-shim/src/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub trait Process {
async fn close_io(&mut self) -> Result<()>;
async fn pause(&mut self) -> Result<()>;
async fn resume(&mut self) -> Result<()>;
async fn id(&self) -> &str;
}

#[async_trait]
Expand Down Expand Up @@ -123,6 +124,10 @@ where
self.pid
}

async fn id(&self) -> &str {
self.id.as_str()
}

async fn state(&self) -> Result<StateResponse> {
let mut resp = StateResponse::new();
resp.id = self.id.to_string();
Expand Down
48 changes: 26 additions & 22 deletions crates/runc-shim/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ async fn process_exits(
let exit_code = e.exit_code;
for (_k, cont) in containers.write().await.iter_mut() {
let bundle = cont.bundle.to_string();
let container_id = cont.id.clone();
let mut change_process: Vec<&mut (dyn Process + Send + Sync)> = Vec::new();
// pid belongs to container init process
if cont.init.pid == pid {
// kill all children process if the container has a private PID namespace
Expand All @@ -171,20 +173,30 @@ async fn process_exits(
error!("failed to kill init's children: {}", e)
});
}
// set exit for init process
cont.init.set_exited(exit_code).await;

if let Ok(process_d) = cont.get_mut_process(None) {
change_process.push(process_d);
} else {
break;
}
} else {
// pid belongs to container common process
if let Some((_, p)) = cont.processes.iter_mut().find(|(_, p)| p.pid == pid)
{
change_process.push(p as &mut (dyn Process + Send + Sync));
}
}
let process_len = change_process.len();
for process in change_process {
// set exit for process
process.set_exited(exit_code).await;
let code = process.exit_code().await;
let exited_at = process.exited_at().await;
// publish event
let (_, code, exited_at) = match cont.get_exit_info(None).await {
Ok(info) => info,
Err(_) => break,
};

let ts = convert_to_timestamp(exited_at);
let event = TaskExit {
container_id: cont.id.to_string(),
id: cont.id.to_string(),
pid: cont.pid().await as u32,
container_id: container_id.clone(),
id: process.id().await.to_string(),
pid: process.pid().await as u32,
exit_status: code as u32,
exited_at: Some(ts).into(),
..Default::default()
Expand All @@ -193,18 +205,10 @@ async fn process_exits(
tx.send((topic.to_string(), Box::new(event)))
.await
.unwrap_or_else(|e| warn!("send {} to publisher: {}", topic, e));

break;
}

// pid belongs to container common process
for (_exec_id, p) in cont.processes.iter_mut() {
// set exit for exec process
if p.pid == pid {
p.set_exited(exit_code).await;
// TODO: publish event
break;
}
//if process has been find , no need to keep search
if process_len != 0 {
break;
}
}
}
Expand Down

0 comments on commit eef8806

Please sign in to comment.