runtime-rs: Introduce a helper to check normal oom shutdown errors

It mainly for checking if an error is a normal oom shutdown error
due to network disconnection issues.

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn
2026-01-06 19:32:35 +08:00
parent 1af9456e26
commit 59677688ee

View File

@@ -21,3 +21,33 @@ pub enum Error {
#[error("process already terminated")]
ProcessAlreadyTerminated,
}
/// Common error messages indicating normal OOM shutdowns due to network issues.
const NORMAL_OOM_SHUTDOWN_MESSAGES: &[&str] = &[
"Connection reset by peer",
"Broken pipe",
"transport endpoint is not connected",
];
/// Checks if an error indicates a normal oom shutdown due to network disconnections.
///
/// This function identifies errors that commonly occur when a connection is gracefully
/// or unexpectedly terminated by the peer, such as network interruptions or the remote
/// end closing the connection.
pub fn is_normal_oom_shutdown_error(err: &anyhow::Error) -> bool {
// Check for common I/O error kinds that indicate connection issues.
if let Some(io_err) = err.downcast_ref::<std::io::Error>() {
match io_err.kind() {
std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::BrokenPipe
| std::io::ErrorKind::NotConnected => return true,
_ => {}
}
}
// Additionally, check the error message for specific substrings.
let error_string = err.to_string().to_lowercase();
NORMAL_OOM_SHUTDOWN_MESSAGES
.iter()
.any(|pattern| error_string.contains(&pattern.to_lowercase()))
}