Skip to content

fix(cloudfetch): enforce end-to-end download deadline for chunks - #323

Merged
vikrantpuppala merged 2 commits into
mainfrom
fix/321-cloudfetch-download-deadline
Sep 6, 2026
Merged

vikrantpuppala merged 2 commits into
mainfrom
fix/321-cloudfetch-download-deadline

Conversation

@vikrantpuppala

Copy link
Copy Markdown
Collaborator

Summary

Fixes audit finding H05 (High) — CloudFetch chunk downloads had no absolute wall-clock deadline, so a single query could block for many minutes.

Root cause. The per-session config already declared a 20s CloudFetch budget (overall_timeout in build_retry_configs_for_session), but it was set alongside max_retries: 0. That budget is only ever consulted in execute_impl as a pre-retry-sleep gate — with zero retries there are no sleeps, so for CloudFetch the 20s was dead code. Each attempt was instead bounded only by the static per-request timeout (request_timeout, default 120s), and the provider's own retry loop (max_retries=5, retry_delay=1500ms) called download() up to 5×. Worst case, a permanently-stuck chunk blocked the consumer for ~5 × 120s ≈ 10 minutes on a nominal budget — exactly the reported pathology.

Fix

Make the already-declared budget real and enforce it end-to-end (no new public config surface):

  • Add CloudFetchConfig::download_timeout — the absolute per-chunk budget — threaded into download_chunk_with_retry via DownloadCtx.
  • Compute an Instant deadline at loop entry; check it at the top of every attempt and stop promptly with a stable Error::timeout() classification when spent. Cap the backoff sleep at the remaining budget so it can't overshoot.
  • Cap each HTTP attempt at min(request_timeout, remaining) via reqwest's per-request timeout, which spans connect → response headers → response body — so a stall in any phase is bounded by the deadline, not the static 120s.
  • Preserve a caller's per-request timeout across execute_impl's per-attempt request rebuild; it was previously dropped, which would have silently defeated the per-attempt cap.
  • Source the provider budget and the CloudFetch HTTP overall_timeout from one shared constant (CLOUDFETCH_DOWNLOAD_BUDGET = 20s) so the two layers can't drift.

A deadline-exceeded chunk records ChunkState::DownloadFailed, so wait_for_chunk observes it and returns — the consumer is now unblocked within the budget instead of hanging.

Acceptance criteria mapping

  • ✅ Absolute end-to-end deadline (download_timeout, enforced as an Instant).
  • ✅ Remaining budget propagated into every retry/backoff/HTTP attempt (per-attempt cap + capped sleep + preserved-across-rebuild timeout).
  • ✅ Each attempt capped at the remaining total budget.
  • ✅ Deadline applied while waiting for headers and while consuming the body (reqwest per-request .timeout() spans both).
  • ✅ Stable, prompt timeout classification (Error::timeout(), ErrorCode::Timeout).
  • ✅ Deterministic tests for stalled body, retry/backoff exhaustion, and cancellation near the deadline.
  • ✅ Elapsed stays within a documented scheduling tolerance of the budget (asserted in tests).

Test plan

New download_deadline test module in streaming_provider.rs (real ChunkDownloader against a wiremock server, so reqwest's per-request timeout is genuinely exercised):

  • stalled_body_is_bounded_by_deadline — a 30s-delayed response is cut off by a 300ms budget; asserts Timeout classification and elapsed within tolerance. Verified this test fails (hangs → outer guard) when the per-request-timeout preservation is removed, proving it catches the bug.
  • retry_exhaustion_is_bounded_by_deadline — persistent fast HTTP 500 with a 10 000-retry cap stops at a 250ms deadline (far fewer than the attempt cap).
  • cancel_under_live_deadline_returns_promptly — cancellation under a 10s budget is observed at the next loop boundary and returns in ~50ms.

Config/wiring coverage:

  • types/cloudfetch.rs: default download_timeout == 20s.
  • session.rs: cloudfetch_provider_and_http_budgets_are_unified asserts the provider download_timeout and the HTTP CloudFetch overall_timeout are the same shared constant.

Verification (local, --no-default-features --features tls-rustls):

  • cargo test --lib: 925 pass; the 1 failure (c_abi::...::mtls_identity_is_forwarded_when_session_opens) is a pre-existing rustls-only artifact (asserts the native-tls parser message) that fails identically on clean main — not a regression. CI builds each TLS feature separately.
  • cargo test --doc: 9 pass.
  • cargo fmt --all, cargo clippy --all-targets: clean.

Related (out of scope — follow-up)

An investigation of sibling retry/polling loops found the async statement poll (executed_async.rs) and the HTTP retry loop (http.rs) are already deadline-bounded. wait_for_chunk has no independent absolute deadline, but with this fix a stalled download always writes terminal state within the budget, so it is bounded for the H05 case; a residual never-scheduled-chunk edge is a narrower, separate concern.

Closes #321

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium

Solid, well-tested fix for H05 — the end-to-end deadline, per-attempt cap, capped backoff, and shared budget constant are correct and the unblocking behavior works. One medium concern: the promised "stable ErrorCode::Timeout" classification is lost on the real consumer path (wait_for_chunk re-wraps all DownloadFailed as Error::io()), and the new tests only exercise the direct call, not the consumer-observed error.

@vikrantpuppala vikrantpuppala added the engineer-bot Apply to an issue to trigger the engineer-bot; on a PR to enable engineer follow-up label Sep 6, 2026
oss-engineer-bot Bot pushed a commit that referenced this pull request Sep 6, 2026
Addresses:
  - #3942969392 at src/reader/cloudfetch/streaming_provider.rs:773

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Solid, well-tested fix for H05 — the type change (DownloadFailed(String) → DownloadFailed(Error)), the per-request-timeout preservation in execute_impl, the capped backoff, and the shared budget constant are all correct, and the wiremock deadline tests genuinely exercise reqwest's per-request timeout. One Low note: the in-loop link-refresh await isn't bounded by the remaining chunk budget, so the "spans link refresh" claim slightly overstates coverage.

oss-engineer-bot Bot pushed a commit that referenced this pull request Sep 6, 2026
Addresses:
  - #3943311964 at src/reader/cloudfetch/streaming_provider.rs:811

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 High · 1 Medium

Solid, well-tested fix that makes the previously-dead 20s CloudFetch budget a real end-to-end deadline; the typed-error preservation, per-attempt cap, refresh-timeout wrapping, and sleep capping are all correct, and the download() signature change has its single caller updated. Two concerns: (1) the new #[cfg(test)] for_test_with_chunks constructor appears to omit the required terminal_error field (would fail the cargo test build — please verify), and (2) enforcing a flat 20s per-chunk cap is a genuine behavior change that could regress large/slow chunk downloads. No nits.

Comment thread src/session.rs Resolved
CloudFetch chunk downloads had no absolute wall-clock deadline. The
per-session config already declared a 20s CloudFetch budget
(overall_timeout in build_retry_configs_for_session), but with
max_retries=0 that budget was only ever consulted as a pre-retry-sleep
gate in execute_impl — so for CloudFetch it was dead code. Each attempt
was instead bounded only by the static per-request timeout
(request_timeout, default 120s), and the provider's own retry loop
(max_retries=5, retry_delay=1500ms) called download() up to 5 times.
Worst case a permanently-stuck chunk blocked the consumer for
~5 × 120s ≈ 10 minutes on a nominal budget (audit finding H05, High).

Make the declared budget real and end-to-end:

- Add CloudFetchConfig::download_timeout, the absolute per-chunk budget,
  and thread it into download_chunk_with_retry via DownloadCtx.
- Compute an Instant deadline at loop entry; check it at the top of every
  attempt and stop promptly with a stable Error::timeout() classification
  when spent. Cap the backoff sleep at the remaining budget so it can't
  overshoot the deadline.
- Cap each HTTP attempt at min(request_timeout, remaining budget) via
  reqwest's per-request timeout, which spans connect, response headers,
  and response body — so a stall in any phase is bounded by the deadline.
- Bound the in-loop link-refresh RPC by the remaining budget too, so a
  stalled refresh can't overshoot via the SEA category's own timeout.
- Preserve a caller's per-request timeout across execute_impl's
  per-attempt request rebuild; it was previously dropped, which would
  have silently defeated the per-attempt cap.
- Source the provider budget and the CloudFetch HTTP overall_timeout from
  one shared constant (CLOUDFETCH_DOWNLOAD_BUDGET) so the two layers can't
  drift.

Adds deterministic tests (real ChunkDownloader against wiremock) for a
stalled body, retry/backoff exhaustion, and cancellation under a live
deadline, plus a consumer-path test proving the timeout classification
survives through wait_for_chunk to the reader. Each asserts elapsed stays
within tolerance of the budget and the error is classified as a timeout.

Closes #321

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — a well-scoped, correctly-implemented fix for H05 with strong deterministic test coverage (stalled body, retry exhaustion, cancellation, and consumer-path classification). The ChunkState::DownloadFailed(String)→(Error) change, the new download() arg, and the shared-budget constant are all consistently threaded and compile-safe. Only one minor doc-vs-code note (latent, harmless today) raised inline.

Addresses:
  - #3943393388 at src/reader/cloudfetch/streaming_provider.rs:1019

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

This branch was previously deployed

1 inactive deployment
azure-prod — c1921200 Deployed Sep 6, 2026 by oss-reviewer-bot[bot] via followup #1321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engineer-bot Apply to an issue to trigger the engineer-bot; on a PR to enable engineer follow-up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CloudFetch] Enforce hard per-attempt and end-to-end download deadlines

1 participant