|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Shared classification for transport closes that are expected during shutdown. |
| 5 | +
|
| 6 | +use std::io::ErrorKind; |
| 7 | + |
| 8 | +/// Returns `true` when a gRPC status represents the peer closing transport. |
| 9 | +/// |
| 10 | +/// Callers must still decide whether shutdown is already in progress before |
| 11 | +/// downgrading the error. Outside shutdown these statuses remain failures. |
| 12 | +#[must_use] |
| 13 | +pub fn is_expected_transport_close_status(status: &tonic::Status) -> bool { |
| 14 | + matches!( |
| 15 | + status.code(), |
| 16 | + tonic::Code::Cancelled |
| 17 | + | tonic::Code::Unavailable |
| 18 | + | tonic::Code::Unknown |
| 19 | + | tonic::Code::Internal |
| 20 | + ) && is_expected_transport_close_message(&status.to_string()) |
| 21 | +} |
| 22 | + |
| 23 | +/// Returns `true` when an error looks like an expected transport close. |
| 24 | +/// |
| 25 | +/// This helper intentionally matches only transport-lifecycle failures such as |
| 26 | +/// HTTP/2 close races, broken pipes, and reset connections. |
| 27 | +#[must_use] |
| 28 | +pub fn is_expected_transport_close_error(error: &(dyn std::error::Error + 'static)) -> bool { |
| 29 | + if let Some(status) = error.downcast_ref::<tonic::Status>() { |
| 30 | + return is_expected_transport_close_status(status); |
| 31 | + } |
| 32 | + if let Some(io) = error.downcast_ref::<std::io::Error>() { |
| 33 | + return matches!( |
| 34 | + io.kind(), |
| 35 | + ErrorKind::BrokenPipe |
| 36 | + | ErrorKind::ConnectionAborted |
| 37 | + | ErrorKind::ConnectionReset |
| 38 | + | ErrorKind::UnexpectedEof |
| 39 | + ); |
| 40 | + } |
| 41 | + is_expected_transport_close_message(&error.to_string()) |
| 42 | +} |
| 43 | + |
| 44 | +/// Returns `true` for known text fragments emitted by tonic, hyper, h2, and |
| 45 | +/// OS I/O errors when the peer closes a stream or connection. |
| 46 | +#[must_use] |
| 47 | +pub fn is_expected_transport_close_message(message: &str) -> bool { |
| 48 | + let msg = message.to_ascii_lowercase(); |
| 49 | + msg.contains("h2 protocol error") |
| 50 | + || msg.contains("broken pipe") |
| 51 | + || msg.contains("connection reset") |
| 52 | + || msg.contains("connection aborted") |
| 53 | + || msg.contains("connection closed") |
| 54 | + || msg.contains("connection error") |
| 55 | + || msg.contains("error reading a body from connection") |
| 56 | + || msg.contains("peer closed connection") |
| 57 | + || msg.contains("unexpected eof") |
| 58 | + || msg.contains("cancelled") |
| 59 | + || msg.contains("canceled") |
| 60 | +} |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use super::*; |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn classifies_expected_transport_close_statuses() { |
| 68 | + for status in [ |
| 69 | + tonic::Status::unknown("h2 protocol error: error reading a body from connection"), |
| 70 | + tonic::Status::unavailable("connection reset by peer"), |
| 71 | + tonic::Status::cancelled("operation cancelled"), |
| 72 | + tonic::Status::internal("broken pipe"), |
| 73 | + ] { |
| 74 | + assert!( |
| 75 | + is_expected_transport_close_status(&status), |
| 76 | + "status should be expected during shutdown: {status}" |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn keeps_unexpected_statuses_out_of_shutdown_filter() { |
| 83 | + for status in [ |
| 84 | + tonic::Status::internal("policy evaluation failed"), |
| 85 | + tonic::Status::invalid_argument("bad relay frame"), |
| 86 | + tonic::Status::permission_denied("cross-sandbox relay"), |
| 87 | + ] { |
| 88 | + assert!( |
| 89 | + !is_expected_transport_close_status(&status), |
| 90 | + "status should stay unexpected: {status}" |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn classifies_io_transport_closes() { |
| 97 | + for kind in [ |
| 98 | + ErrorKind::BrokenPipe, |
| 99 | + ErrorKind::ConnectionAborted, |
| 100 | + ErrorKind::ConnectionReset, |
| 101 | + ErrorKind::UnexpectedEof, |
| 102 | + ] { |
| 103 | + let error = std::io::Error::new(kind, "peer closed"); |
| 104 | + assert!(is_expected_transport_close_error(&error)); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments