From 0ffdc576d358b91036ca41065690661a76cdf903 Mon Sep 17 00:00:00 2001 From: Alex Lyn Date: Tue, 6 Jan 2026 19:35:20 +0800 Subject: [PATCH] runtime-rs: Introduce a helper to check if process/container exists Returns `true` if the error indicates that the target process/container no longer exists. This is used to determine if an operation, like signaling a process, failed because the target is no longer available. The function checks for standard OS error codes (`ESRCH`, `ENOENT`) and common error message patterns. Signed-off-by: Alex Lyn --- .../crates/runtimes/common/src/error.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/runtime-rs/crates/runtimes/common/src/error.rs b/src/runtime-rs/crates/runtimes/common/src/error.rs index b2f7e1ce3b..4268dd4495 100644 --- a/src/runtime-rs/crates/runtimes/common/src/error.rs +++ b/src/runtime-rs/crates/runtimes/common/src/error.rs @@ -4,6 +4,8 @@ // SPDX-License-Identifier: Apache-2.0 // +use nix::libc; + use crate::types::{ContainerProcess, SandboxResponse, TaskResponse}; #[derive(thiserror::Error, Debug)] @@ -51,3 +53,33 @@ pub fn is_normal_oom_shutdown_error(err: &anyhow::Error) -> bool { .iter() .any(|pattern| error_string.contains(&pattern.to_lowercase())) } + +/// List of common error message patterns indicating that a process or container is missing. +const NO_SUCH_PROCESS_MESSAGES: &[&str] = &[ + "no such process", + "process not found", + "init process not found", + "cannot find init process", +]; + +/// Returns `true` if the error indicates that the target process/container no longer exists. +/// This is used to determine if an operation, like signaling a process, failed because the +/// target is no longer available. +/// The function checks for standard OS error codes (`ESRCH`, `ENOENT`) and common error message patterns. +pub fn is_no_such_process_error(err: &anyhow::Error) -> bool { + // Check for standard OS error codes. + if let Some(io_err) = err.downcast_ref::() { + if let Some(raw_os_error) = io_err.raw_os_error() { + // standard "no such process" error. + if raw_os_error == libc::ESRCH || raw_os_error == libc::ENOENT { + return true; + } + } + } + + // Fallback to checking the error message for known patterns. + let error_string = err.to_string().to_lowercase(); + NO_SUCH_PROCESS_MESSAGES + .iter() + .any(|pattern| error_string.contains(&pattern.to_lowercase())) +}