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 committed Dec 12, 2024
1 parent 8029a61 commit 652639d
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion crates/runc-shim/src/container.rs
Original file line number Diff line number Diff line change
@@ -18,11 +18,12 @@ use std::collections::HashMap;

use async_trait::async_trait;
use containerd_shim::{
api::Status,
error::Result,
protos::{
api::{CreateTaskRequest, ExecProcessRequest, ProcessInfo, StateResponse},
cgroups::metrics::Metrics,
protobuf::{well_known_types::any::Any, Message, MessageDyn},
protobuf::{well_known_types::any::Any, EnumOrUnknown, Message, MessageDyn},
shim::oci::ProcessDetails,
},
Error,
@@ -58,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]
@@ -95,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?;
14 changes: 11 additions & 3 deletions crates/runc-shim/src/task.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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();

0 comments on commit 652639d

Please sign in to comment.