mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-29 20:24:31 +00:00
agent: avoid unwrap() in function do_remove_container()
Avoid unwrap() in function do_remove_container(), and also make implmementation symmetric for both timeout and non-timeout cases. Signed-off-by: Jiang Liu <gerry@linux.alibaba.com>
This commit is contained in:
parent
84badd89d7
commit
71a9f67781
@ -289,45 +289,38 @@ impl AgentService {
|
||||
|
||||
if req.timeout == 0 {
|
||||
let mut sandbox = self.sandbox.lock().await;
|
||||
|
||||
sandbox.bind_watcher.remove_container(&cid).await;
|
||||
|
||||
sandbox
|
||||
.get_container(&cid)
|
||||
.ok_or_else(|| anyhow!("Invalid container id"))?
|
||||
.destroy()
|
||||
.await?;
|
||||
|
||||
remove_container_resources(&mut sandbox, &cid)?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// timeout != 0
|
||||
let s = self.sandbox.clone();
|
||||
let cid2 = cid.clone();
|
||||
let (tx, rx) = tokio::sync::oneshot::channel::<i32>();
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
let mut sandbox = s.lock().await;
|
||||
if let Some(ctr) = sandbox.get_container(&cid2) {
|
||||
ctr.destroy().await.unwrap();
|
||||
sandbox.bind_watcher.remove_container(&cid2).await;
|
||||
tx.send(1).unwrap();
|
||||
};
|
||||
match sandbox.get_container(&cid2) {
|
||||
Some(ctr) => ctr.destroy().await,
|
||||
None => Err(anyhow!("Invalid container id")),
|
||||
}
|
||||
});
|
||||
|
||||
if tokio::time::timeout(Duration::from_secs(req.timeout.into()), rx)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return Err(anyhow!(nix::Error::ETIME));
|
||||
}
|
||||
handle.await?;
|
||||
|
||||
let to = Duration::from_secs(req.timeout.into());
|
||||
match tokio::time::timeout(to, handle).await {
|
||||
Ok(res) => {
|
||||
res??;
|
||||
let mut sandbox = self.sandbox.lock().await;
|
||||
remove_container_resources(&mut sandbox, &cid)
|
||||
}
|
||||
Err(_e) => Err(anyhow!(nix::Error::ETIME)),
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn do_exec_process(&self, req: protocols::agent::ExecProcessRequest) -> Result<()> {
|
||||
|
Loading…
Reference in New Issue
Block a user