diff --git a/src/agent/src/rpc.rs b/src/agent/src/rpc.rs index c7d60f1e6b..9cef20c07c 100644 --- a/src/agent/src/rpc.rs +++ b/src/agent/src/rpc.rs @@ -1040,6 +1040,10 @@ impl protocols::agent_ttrpc::AgentService for AgentService { // destroy all containers, clean up, notify agent to exit // etc. sandbox.destroy().await.unwrap(); + // Close get_oom_event connection, + // otherwise it will block the shutdown of ttrpc. + sandbox.event_tx.take(); + sandbox.sender.take().unwrap().send(1).unwrap(); Ok(Empty::new()) @@ -1188,15 +1192,16 @@ impl protocols::agent_ttrpc::AgentService for AgentService { drop(s); drop(sandbox); - match event_rx.recv().await { - None => Err(ttrpc_error(ttrpc::Code::INTERNAL, "")), - Some(container_id) => { - info!(sl!(), "get_oom_event return {}", &container_id); - let mut resp = OOMEvent::new(); - resp.container_id = container_id; - Ok(resp) - } + if let Some(container_id) = event_rx.recv().await { + info!(sl!(), "get_oom_event return {}", &container_id); + + let mut resp = OOMEvent::new(); + resp.container_id = container_id; + + return Ok(resp); } + + Err(ttrpc_error(ttrpc::Code::INTERNAL, "")) } } diff --git a/src/agent/src/sandbox.rs b/src/agent/src/sandbox.rs index cadf6b71c3..9d5244ee39 100644 --- a/src/agent/src/sandbox.rs +++ b/src/agent/src/sandbox.rs @@ -47,7 +47,7 @@ pub struct Sandbox { pub rtnl: Handle, pub hooks: Option, pub event_rx: Arc>>, - pub event_tx: Sender, + pub event_tx: Option>, } impl Sandbox { @@ -76,7 +76,7 @@ impl Sandbox { rtnl: Handle::new()?, hooks: None, event_rx, - event_tx: tx, + event_tx: Some(tx), }) } @@ -311,9 +311,18 @@ impl Sandbox { } pub async fn run_oom_event_monitor(&self, mut rx: Receiver, container_id: String) { - let tx = self.event_tx.clone(); let logger = self.logger.clone(); + if self.event_tx.is_none() { + error!( + logger, + "sandbox.event_tx not found in run_oom_event_monitor" + ); + return; + } + + let tx = self.event_tx.as_ref().unwrap().clone(); + tokio::spawn(async move { loop { let event = rx.recv().await;