Skip to content

fix(cloudfetch): propagate link-fetch failures as terminal stream errors - #322

Merged
vikrantpuppala merged 2 commits into
mainfrom
fix/319-cloudfetch-terminal-link-errors
Sep 6, 2026
Merged

vikrantpuppala merged 2 commits into
mainfrom
fix/319-cloudfetch-terminal-link-errors

Conversation

@vikrantpuppala

Copy link
Copy Markdown
Collaborator

Summary

CloudFetch link-fetch failures (initial or incremental) were logged and signalled with notify_one(), but never persisted as a terminal provider state. A link fetch fails before any ChunkEntry for the requested chunk exists, and it leaves end_of_stream unset — so a consumer woken in wait_for_chunk found neither data nor a terminal error and looped forever, hanging the query indefinitely and tying up application threads (audit finding H03).

Fix

Add a provider-level terminal_error: Mutex<Option<Error>>. The two link-fetch error paths (initialize and fetch_and_store_links) now record the first failure via set_terminal_error, which:

  • keeps the first error (first-writer-wins) so the original context — message, SQLSTATE, source chain — is preserved;
  • wakes every current waiter with notify_waiters() instead of notify_one(), so a terminal failure never strands a sibling waiter.

wait_for_chunk checks the terminal error at the top of its loop (before the per-chunk / end-of-stream branches), and next_batch prefers it over the generic "not found" message — so current and future consumers return a deterministic error instead of waiting again.

Scope: per-chunk download/processing failures already surface via the chunk entry's DownloadFailed / ProcessingFailed state (which wait_for_chunk observes), and the inline provider propagates errors through its mpsc channel — neither has this hang class. The fix is deliberately scoped to the two link-fetch paths named in the issue.

Acceptance criteria

  • Persist the first terminal provider failure after retry policy is exhausted (first-writer-wins in set_terminal_error).
  • Wake every current waiter when the provider enters the terminal state (notify_waiters()).
  • Make current and future consumers return the terminal error instead of waiting again (wait_for_chunk + next_batch checks).
  • Preserve the original error context and expose deterministic failure semantics (message + SQLSTATE asserted in tests).
  • Fault-injection coverage for initial link fetch, incremental link fetch, pagination/refill, waiters registered before failure, and reads started after failure.
  • Cancellation racing with a terminal link-fetch failure, proving no waiter remains blocked.

Test plan

New tests in src/reader/cloudfetch/streaming_provider.rs:

  • initial_link_fetch_failure_is_terminal — first next_batch returns the terminal error (message + SQLSTATE preserved), no hang.
  • incremental_link_fetch_failure_is_terminal — a failure on a later page becomes terminal; no read hangs; no false clean-EOS.
  • waiter_registered_before_failure_is_woken — a consumer already parked in wait_for_chunk is woken and returns the error.
  • read_started_after_failure_returns_terminal_error — a read after the terminal state returns immediately (no wait).
  • cancel_racing_terminal_failure_leaves_no_waiter_blocked — cancel racing the failure leaves no waiter blocked.
  • first_terminal_error_wins — a later, different failure does not overwrite the persisted error.

Each hang test is guarded by an outer tokio::time::timeout; with the fix reverted the tests fail via Elapsed, confirming they reproduce the indefinite hang. Verified locally.

  • cargo test --no-default-features --features tls-rustls --lib --tests — 927 pass (the one failure, mtls_identity_is_forwarded_when_session_opens, is a pre-existing tls-rustls-only artifact unrelated to this change; it fails identically on main).
  • cargo clippy --no-default-features --features tls-rustls --all-targets -- -D warnings — clean.
  • cargo fmt --all — applied.

Closes #319

@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 · 1 Low

Solid, well-tested fix for the CloudFetch link-fetch hang (H03) — the terminal-error state, first-writer-wins, and notify_waiters() are correct, and the std Mutex is never held across an .await. Two concerns: the terminal_error() check precedes the ready-chunk check in wait_for_chunk, so a later-page failure can discard already-downloaded in-order chunks (medium); and the incremental test doesn't isolate the path it claims to cover (low).

CloudFetch link-fetch failures (initial or incremental) were logged and
signalled with `notify_one()`, but never persisted as a terminal state.
A link fetch fails *before* any `ChunkEntry` for the requested chunk
exists, and it leaves `end_of_stream` unset, so a consumer woken in
`wait_for_chunk` found neither data nor a terminal error and looped
forever — hanging the query indefinitely and tying up application
threads (audit finding H03).

Add a provider-level `terminal_error: Mutex<Option<Error>>`. The two
link-fetch error paths (`initialize` and `fetch_and_store_links`) now
record the first failure via `set_terminal_error`, which:

- keeps the first error (first-writer-wins) so the original context
  (message, SQLSTATE, source chain) is preserved;
- wakes *every* current waiter with `notify_waiters()` instead of
  `notify_one()`, so a terminal failure never strands a sibling waiter.

`wait_for_chunk` consults the terminal error only *after* the
ready-chunk check and only when the requested chunk has no entry and
the stream is not at a clean end. The download frontier runs ahead of
the consumer by up to `max_chunks_in_memory`, so a later-page link
failure must not discard chunks that are already downloaded and ready
in order; the consumer drains those first and observes the terminal
error only once it reaches the chunk whose link failed — matching the
per-chunk `DownloadFailed`/`ProcessingFailed` path. `next_batch` also
prefers the terminal error over the generic "not found" fall-through.

Per-chunk download/processing failures already surface via the chunk
entry's state, and the inline provider propagates errors through its
mpsc channel, so neither has this hang class; the fix is scoped to the
two link-fetch paths.

Adds fault-injection tests for initial-link, incremental-link, and
refill failures; a waiter registered before the failure; a read started
after the failure; cancellation racing a terminal failure; and
first-writer-wins. The incremental test serves chunk 0's bytes from a
mock HTTP server so the download succeeds and the *only* failure source
is the incremental link fetch — it asserts chunk 0's rows are delivered
in order before the terminal error (guarding against the in-order
discard) and that the surfaced error carries the injected message +
SQLSTATE. Each hang test is guarded by an outer timeout that, with the
fix reverted, fails via `Elapsed`.

Closes #319

Co-authored-by: Isaac <no-reply@databricks.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 — the fix correctly closes the H03 hang. A provider-level terminal_error is persisted first-writer-wins, checked at the top of the bounded wait_for_chunk loop and in next_batch, and broadcast via notify_waiters(); link-fetch failures never set end_of_stream, sequential link fetching preserves in-order delivery of already-downloaded chunks, and the tests cover initial/incremental/pre-registered-waiter/after-failure/cancel-race/first-wins cases. One low-severity note: the notified() future is registered after the terminal-error check, so a missed notify_waiters() is only rescued by the (default 30s) timeout rather than being race-free.

@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
Addresses:
  - #3942713141 at src/reader/cloudfetch/streaming_provider.rs:551

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.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Sep 6, 2026
Merged via the queue into main with commit 73ef087 Sep 6, 2026
23 of 26 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — 8375fe0a Deployed Sep 6, 2026 by oss-reviewer-bot[bot] via followup #977
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] Propagate link-fetch failures as terminal stream errors

1 participant