Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix exec hang when tx channel is full #316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
81 changes: 40 additions & 41 deletions crates/runc-shim/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,52 +159,51 @@ async fn process_exits(
if let Subject::Pid(pid) = e.subject {
debug!("receive exit event: {}", &e);
let exit_code = e.exit_code;
for (_k, cont) in containers.lock().await.iter_mut() {
let bundle = cont.bundle.to_string();
// pid belongs to container init process
if cont.init.pid == pid {
// kill all children process if the container has a private PID namespace
if should_kill_all_on_exit(&bundle).await {
cont.kill(None, 9, true).await.unwrap_or_else(|e| {
error!("failed to kill init's children: {}", e)
});
}
// set exit for init process
cont.init.set_exited(exit_code).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,
exit_status: code as u32,
exited_at: Some(ts).into(),
..Default::default()
};
let topic = event.topic();
tx.send((topic.to_string(), Box::new(event)))
.await
.unwrap_or_else(|e| warn!("send {} to publisher: {}", topic, e));
let containers = containers.clone();
let tx = tx.clone();
tokio::spawn(async move {
for (_k, cont) in containers.lock().await.iter_mut() {
let bundle = cont.bundle.to_string();
// pid belongs to container init process
if cont.init.pid == pid {
// kill all children process if the container has a private PID namespace
if should_kill_all_on_exit(&bundle).await {
cont.kill(None, 9, true).await.unwrap_or_else(|e| {
error!("failed to kill init's children: {}", e)
});
}
// set exit for init process
cont.init.set_exited(exit_code).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,
exit_status: code as u32,
exited_at: Some(ts).into(),
..Default::default()
};
let topic = event.topic();
tx.send((topic.to_string(), Box::new(event)))
.await
.unwrap_or_else(|e| warn!("send {} to publisher: {}", topic, e));

break;
}
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 {
// pid belongs to container common process
if let Some(p) = cont.processes.values_mut().find(|p| p.pid == pid) {
p.set_exited(exit_code).await;
// TODO: publish event
break;
}
}
}
});
}
}
monitor_unsubscribe(s.id).await.unwrap_or_default();
Expand Down
9 changes: 5 additions & 4 deletions crates/shim/src/asynchronous/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::HashMap;
use lazy_static::lazy_static;
use log::error;
use tokio::sync::{
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
mpsc::{channel, Receiver, Sender},
Mutex,
};

Expand Down Expand Up @@ -68,17 +68,17 @@ pub struct Monitor {

pub(crate) struct Subscriber {
pub(crate) topic: Topic,
pub(crate) tx: UnboundedSender<ExitEvent>,
pub(crate) tx: Sender<ExitEvent>,
}

pub struct Subscription {
pub id: i64,
pub rx: UnboundedReceiver<ExitEvent>,
pub rx: Receiver<ExitEvent>,
}

impl Monitor {
pub fn subscribe(&mut self, topic: Topic) -> Result<Subscription> {
let (tx, rx) = unbounded_channel::<ExitEvent>();
let (tx, rx) = channel::<ExitEvent>(128);
let id = self.seq_id;
self.seq_id += 1;
let subscriber = Subscriber {
Expand Down Expand Up @@ -117,6 +117,7 @@ impl Monitor {
subject: subject.clone(),
exit_code,
})
.await
.map_err(other_error!(e, "failed to send exit code"));
results.push(res);
}
Expand Down
Loading