From 8fccb4c7d1f0e1ee527651a7a8139ecdf1599dae Mon Sep 17 00:00:00 2001 From: Vikrant Puppala Date: Mon, 10 Aug 2026 11:06:07 +0000 Subject: [PATCH] fix(retry): correct retry_max_wait < retry_min_wait instead of panicking (#214) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A backoff-bound tuning typo where `retry_max_wait < retry_min_wait` was dormant until the first retry, then panicked in `calculate_backoff`: `wait.clamp(min_wait, max_wait)` hits `Duration`'s `Ord::clamp`, which asserts `min <= max`. On a transient 503 this aborted the caller's task with an unhandleable panic instead of returning a `Result`. Correct the pair (raise max to min) with a warning, rather than reject or panic — a backoff bound is a tuning hint, matching the clamp-and-warn discipline `apply_client_result_overrides` already uses: - `calculate_backoff` now uses an effective ceiling `max_wait.max(min_wait)` so no config can panic it (silent on this per-retry hot path). - `build_retry_configs_for_session` emits the one-time correction `warn!` and raises max to min when building the per-session `RetryConfig`. - C-ABI `kernel_session_config_set_retry_config` now corrects-and-warns instead of rejecting `max_wait_ms < min_wait_ms` with `InvalidArgument`, aligning it with the Rust path and the reference ODBC driver. The `min_wait_ms == 0` rejection stays (a zero floor is genuinely invalid). Closes #214 This pull request and its description were written by Isaac. Signed-off-by: Vikrant Puppala --- src/c_abi/session.rs | 64 ++++++++++++++++++++++++++++++++--------- src/client/retry.rs | 42 +++++++++++++++++++++++++-- src/session.rs | 22 ++++++++++++-- tests/c/session_smoke.c | 6 ++-- 4 files changed, 114 insertions(+), 20 deletions(-) diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index ef52396a..4ceb8f03 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -393,9 +393,12 @@ pub unsafe extern "C" fn kernel_session_config_set_custom_header( /// `min_wait_ms` guarantees zero retries (the first backoff already exceeds the /// budget); this is accepted but logged at `warn`. /// -/// `min_wait_ms == 0` and `max_wait_ms < min_wait_ms` are rejected as -/// `InvalidArgument`. Maps to `HttpConfig::retry_min_wait` / -/// `retry_max_wait` / `retry_max_retries` / `overall_timeout`. +/// `min_wait_ms == 0` is rejected as `InvalidArgument` (a zero floor is +/// genuinely invalid, not a correctable typo). A `max_wait_ms < min_wait_ms` +/// pair is *corrected* (max raised to min) with a `warn`, rather than rejected — +/// a backoff bound is a tuning hint and must not fail a host's connect. Maps to +/// `HttpConfig::retry_min_wait` / `retry_max_wait` / `retry_max_retries` / +/// `overall_timeout`. /// /// # Safety /// `config` must be a live config handle. @@ -411,11 +414,24 @@ pub unsafe extern "C" fn kernel_session_config_set_retry_config( if min_wait_ms == 0 { return Err(Error::invalid_argument().message("retry min_wait_ms must be > 0")); } - if max_wait_ms < min_wait_ms { - return Err( - Error::invalid_argument().message("retry max_wait_ms must be >= min_wait_ms") + // A `max_wait_ms < min_wait_ms` pair is a tuning typo, not a fatal + // config error: correct it (raise max to min) and warn, rather than + // rejecting the whole config and failing the host's connect. A backoff + // bound is a tuning hint and the ceiling is the driver's own, so this + // matches the kernel's clamp-and-warn discipline (see + // `build_retry_configs_for_session` / `calculate_backoff`) and the + // reference ODBC driver's handling. + let max_wait_ms = if max_wait_ms < min_wait_ms { + crate::klog::warn!( + min_wait_ms, + max_wait_ms, + "retry max_wait_ms is below min_wait_ms; raising max_wait_ms to \ + min_wait_ms (a backoff bound is a tuning hint, not a hard failure)", ); - } + min_wait_ms + } else { + max_wait_ms + }; // A non-zero budget below the first backoff wait guarantees zero // retries (the client stops before the first sleep — src/client/http.rs). // Accept it (the single attempt is still coherent, and a valid-looking @@ -1274,27 +1290,47 @@ mod tests { } } - /// The retry setter rejects a zero min_wait and a max_wait below min_wait - /// with `InvalidArgument` — a degenerate range would otherwise reach the - /// HTTP client as nonsense backoff bounds. + /// The retry setter rejects a zero min_wait with `InvalidArgument` (a zero + /// floor is genuinely invalid), but a `max_wait < min_wait` pair is a tuning + /// typo that is CORRECTED (max raised to min) with a warning rather than + /// rejected — a backoff bound must not fail a host's connect + /// (ERRORRECOV-014). #[test] - fn set_retry_config_rejects_bad_range() { + fn set_retry_config_rejects_zero_min_corrects_bad_range() { unsafe { let mut config: *mut KernelSessionConfig = std::ptr::null_mut(); assert_eq!( kernel_session_config_new(&mut config), KernelStatusCode::Success ); + + // Zero min_wait is still rejected outright. assert_eq!( kernel_session_config_set_retry_config(config, 0, 1_000, 3, 0), KernelStatusCode::InvalidArgument ); + // A rejected call leaves the config's retry unset. + assert!(config.as_ref().unwrap().retry.is_none()); + + // max_wait (1s) < min_wait (5s): accepted, and max is raised to min. assert_eq!( kernel_session_config_set_retry_config(config, 5_000, 1_000, 3, 0), - KernelStatusCode::InvalidArgument + KernelStatusCode::Success ); - // A rejected call leaves the config's retry unset. - assert!(config.as_ref().unwrap().retry.is_none()); + let retry = config + .as_ref() + .unwrap() + .retry + .as_ref() + .expect("retry spec should be recorded after correction"); + assert_eq!(retry.min_wait, Duration::from_millis(5_000)); + assert_eq!( + retry.max_wait, + Duration::from_millis(5_000), + "max_wait should be corrected up to min_wait" + ); + assert_eq!(retry.max_retries, 3); + kernel_session_config_free(config); } } diff --git a/src/client/retry.rs b/src/client/retry.rs index 3b5a99cb..8ceedf0f 100644 --- a/src/client/retry.rs +++ b/src/client/retry.rs @@ -304,13 +304,22 @@ pub fn calculate_backoff( attempt: u32, retry_after_header: Option<&str>, ) -> Duration { + // Effective ceiling: never below the floor. A misconfigured + // `max_wait < min_wait` (a tuning typo, e.g. via `Session::builder().http()`) + // would otherwise make `Duration::clamp` panic (`Ord::clamp` asserts + // `min <= max`) on the first retry. Correct the pair by raising max to min + // rather than aborting the caller's task; the one-time warning is emitted + // where the per-session `RetryConfig` is built (see `session.rs`), not on + // this per-retry hot path. + let max_wait = config.max_wait.max(config.min_wait); + let base_wait = if let Some(wait) = retry_after_header.and_then(parse_retry_after) { // Clamp Retry-After to [min_wait, max_wait] - wait.clamp(config.min_wait, config.max_wait) + wait.clamp(config.min_wait, max_wait) } else { // Exponential backoff: min_wait * 2^attempt (per spec) let exp = config.min_wait.saturating_mul(2u32.saturating_pow(attempt)); - exp.min(config.max_wait) + exp.min(max_wait) }; // Proportional jitter: random 5-25% of base_wait, minimum 50ms. @@ -677,6 +686,35 @@ mod tests { assert!(backoff <= Duration::from_millis(12_500)); } + #[test] + fn test_calculate_backoff_max_below_min_does_not_panic() { + // ERRORRECOV-014: a tuning typo where max_wait < min_wait must be + // CORRECTED, not fatal. `Duration`'s `Ord::clamp` panics + // (`assertion failed: min <= max`) when min > max, so the backoff must + // treat the effective ceiling as `max_wait.max(min_wait)` — i.e. raise + // max up to min. The bound stays a tuning hint; a typo must never abort + // the caller's task the first time a transient 503 fires. + let config = RetryConfig { + min_wait: Duration::from_secs(3), + max_wait: Duration::from_secs(1), // deliberately below min + ..Default::default() + }; + + // Retry-After path: 5s clamps to [3s, effective-max=3s] = 3s + jitter. + let backoff = calculate_backoff(&config, 1, Some("5")); + assert!( + backoff >= Duration::from_secs(3), + "Retry-After backoff {backoff:?} should be >= min_wait (3s)" + ); + + // Exponential path: base 3s*2 capped to effective-max 3s + jitter. + let backoff = calculate_backoff(&config, 1, None); + assert!( + backoff >= Duration::from_secs(3), + "exponential backoff {backoff:?} should be >= min_wait (3s)" + ); + } + #[test] fn test_retry_after_invalid_falls_back_to_exponential() { let config = RetryConfig { diff --git a/src/session.rs b/src/session.rs index 36bde53e..8c9050d3 100644 --- a/src/session.rs +++ b/src/session.rs @@ -883,9 +883,27 @@ fn build_proxy_config(proxy: Option<&ProxyConfig>) -> HttpProxyConfig { fn build_retry_configs_for_session( config: &SessionConfig, ) -> std::collections::HashMap { + // A tuning typo where `retry_max_wait < retry_min_wait` is CORRECTED, not + // fatal: raise max to min and warn once here (the single per-session build + // point), rather than letting it reach `calculate_backoff` — where an + // uncorrected pair would otherwise panic `Duration::clamp`. Mirrors the + // clamp-and-warn discipline `apply_client_result_overrides` uses for + // `cloudfetch_max_chunks_in_memory`. + let min_wait = config.http.retry_min_wait; + let mut max_wait = config.http.retry_max_wait; + if max_wait < min_wait { + crate::klog::warn!( + retry_min_wait = ?min_wait, + retry_max_wait = ?max_wait, + "retry_max_wait is below retry_min_wait; raising retry_max_wait to \ + retry_min_wait (a backoff bound is a tuning hint, not a hard failure)", + ); + max_wait = min_wait; + } + let global = RetryConfig { - min_wait: config.http.retry_min_wait, - max_wait: config.http.retry_max_wait, + min_wait, + max_wait, max_retries: config.http.retry_max_retries, overall_timeout: config.http.overall_timeout, ..RetryConfig::default() diff --git a/tests/c/session_smoke.c b/tests/c/session_smoke.c index af383848..ca55e1ae 100644 --- a/tests/c/session_smoke.c +++ b/tests/c/session_smoke.c @@ -167,7 +167,9 @@ static void test_proxy_and_tls_setters(void) { /* Retry config: a valid range (incl. max_retries=0 to disable, overall * timeout 0 = keep default) succeeds; a non-zero overall timeout also - * succeeds; a zero min_wait or an inverted range is rejected. */ + * succeeds; a zero min_wait is rejected; an inverted range (max