Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions src/c_abi/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
42 changes: 40 additions & 2 deletions src/client/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 20 additions & 2 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -883,9 +883,27 @@ fn build_proxy_config(proxy: Option<&ProxyConfig>) -> HttpProxyConfig {
fn build_retry_configs_for_session(
config: &SessionConfig,
) -> std::collections::HashMap<RequestCategory, RetryConfig> {
// 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()
Expand Down
6 changes: 4 additions & 2 deletions tests/c/session_smoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,17 @@ 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<min) is a
* tuning typo that is corrected (max raised to min) with a warning, not
* rejected — a backoff bound must not fail a host's connect. */
rc = kernel_session_config_set_retry_config(config, 500, 30000, 0, 0);
CHECK(rc == KernelStatusCode_Success, "set_retry_config (valid, disable, default budget) succeeds");
rc = kernel_session_config_set_retry_config(config, 500, 30000, 3, 120000);
CHECK(rc == KernelStatusCode_Success, "set_retry_config (valid, overall_timeout=120s) succeeds");
rc = kernel_session_config_set_retry_config(config, 0, 1000, 3, 0);
CHECK(rc == KernelStatusCode_InvalidArgument, "set_retry_config(min=0) → InvalidArgument");
rc = kernel_session_config_set_retry_config(config, 5000, 1000, 3, 0);
CHECK(rc == KernelStatusCode_InvalidArgument, "set_retry_config(max<min) → InvalidArgument");
CHECK(rc == KernelStatusCode_Success, "set_retry_config(max<min) corrected, not rejected");

kernel_session_config_free(config);
}
Expand Down
Loading