Merge pull request #10045 from lifupan/cleanup_container

runtime-rs: container: fix the issue of missing cleanup container
This commit is contained in:
Greg Kurz 2024-07-19 16:36:04 +02:00 committed by GitHub
commit dc97f3f540
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

@ -546,6 +546,18 @@ impl Container {
pub async fn spec(&self) -> oci::Spec {
self.spec.clone()
}
pub async fn cleanup(&mut self) -> Result<()> {
let mut inner = self.inner.write().await;
let device_manager = self.resource_manager.get_device_manager().await;
inner
.cleanup_container(
self.container_id.container_id.as_str(),
true,
&device_manager,
)
.await
}
}
fn amend_spec(spec: &mut oci::Spec, disable_guest_seccomp: bool) -> Result<()> {

View File

@ -177,7 +177,7 @@ impl ContainerInner {
}
}
async fn cleanup_container(
pub(crate) async fn cleanup_container(
&mut self,
cid: &str,
force: bool,

View File

@ -70,7 +70,7 @@ impl VirtContainerManager {
impl ContainerManager for VirtContainerManager {
#[instrument]
async fn create_container(&self, config: ContainerConfig, spec: oci::Spec) -> Result<PID> {
let container = Container::new(
let mut container = Container::new(
self.pid,
config.clone(),
spec.clone(),
@ -107,7 +107,14 @@ impl ContainerManager for VirtContainerManager {
}
let mut containers = self.containers.write().await;
container.create(spec).await.context("create")?;
if let Err(e) = container.create(spec).await {
if let Err(inner_e) = container.cleanup().await {
warn!(sl!(), "failed to cleanup container {:?}", inner_e);
}
return Err(e);
}
containers.insert(container.container_id.to_string(), container);
Ok(PID { pid: self.pid })
}