Skip to content

Commit

Permalink
Prevent the init process from exiting and continuing with start
Browse files Browse the repository at this point in the history
No need to wait for runc return an error

Signed-off-by: jokemanfire <[email protected]>
  • Loading branch information
jokemanfire authored and mxpv committed Dec 13, 2024
1 parent eef8806 commit 6526c28
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions crates/runc-shim/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait Container {
async fn close_io(&mut self, exec_id: Option<&str>) -> Result<()>;
async fn pause(&mut self) -> Result<()>;
async fn resume(&mut self) -> Result<()>;
async fn init_state(&self) -> EnumOrUnknown<Status>;
}

#[async_trait]
Expand Down Expand Up @@ -96,6 +97,11 @@ where
E: Process + Send + Sync,
P: ProcessFactory<E> + Send + Sync,
{
async fn init_state(&self) -> EnumOrUnknown<Status> {
// Default should be unknown
self.init.state().await.unwrap_or_default().status
}

async fn start(&mut self, exec_id: Option<&str>) -> Result<i32> {
let process = self.get_mut_process(exec_id)?;
process.start().await?;
Expand Down
14 changes: 11 additions & 3 deletions crates/runc-shim/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ use containerd_shim::{
PidsResponse, StatsRequest, StatsResponse, UpdateTaskRequest,
},
events::task::{TaskCreate, TaskDelete, TaskExecAdded, TaskExecStarted, TaskIO, TaskStart},
protobuf::MessageDyn,
protobuf::{EnumOrUnknown, MessageDyn},
shim_async::Task,
ttrpc,
ttrpc::r#async::TtrpcContext,
ttrpc::{self, r#async::TtrpcContext},
},
util::{convert_to_any, convert_to_timestamp, AsOption},
TtrpcResult,
Expand Down Expand Up @@ -228,6 +227,15 @@ where
async fn start(&self, _ctx: &TtrpcContext, req: StartRequest) -> TtrpcResult<StartResponse> {
info!("Start request for {:?}", &req);
let mut container = self.container_mut(req.id()).await?;
// Prevent the init process from exiting and continuing with start
// Return early to reduce the time it takes to return only when runc encounters an error
if container.init_state().await == EnumOrUnknown::new(Status::STOPPED) {
debug!("container init process has exited, start process should not continue");
return Err(ttrpc::Error::RpcStatus(ttrpc::get_status(
ttrpc::Code::FAILED_PRECONDITION,
format!("container init process has exited {}", container.id().await),
)));
}
let pid = container.start(req.exec_id.as_str().as_option()).await?;

let mut resp = StartResponse::new();
Expand Down

0 comments on commit 6526c28

Please sign in to comment.