diff --git a/Cargo.lock b/Cargo.lock index db2145b8..e3eb7560 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2436,9 +2436,9 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "twox-hash" -version = "2.1.2" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ea3136b675547379c4bd395ca6b938e5ad3c3d20fad76e7fe85f9e0d011419c" +checksum = "8464ec13c3691491391d9fce00f6416c9a48e46972f72d7865688be2080192c9" [[package]] name = "typenum" diff --git a/src/reader/cloudfetch/streaming_provider.rs b/src/reader/cloudfetch/streaming_provider.rs index 5f32cb03..4de607eb 100644 --- a/src/reader/cloudfetch/streaming_provider.rs +++ b/src/reader/cloudfetch/streaming_provider.rs @@ -216,6 +216,38 @@ impl std::fmt::Debug for StreamingCloudFetchProvider { } } +/// Spawn a download future on `runtime_handle` and await its `JoinHandle`, +/// converting a task panic (`JoinError`) into an ordinary error. +/// +/// tokio turns a panic inside a spawned task into a `JoinError`, but only if the +/// handle is awaited. A detached (fire-and-forget) download task that panics is +/// silently swallowed — its chunk then stays `Downloading` forever and the whole +/// stream wedges (PECOBLR-4026). Routing every chunk download through this helper +/// guarantees a panic surfaces as a failed chunk instead of an indefinite hang. +/// +/// Note: this spawns a second (inner) task and awaits its handle, so each chunk +/// download uses two tasks — the outer scheduling task and this inner one. That +/// decouples the download from the outer task: if the outer task were aborted +/// mid-`await`, the inner download would keep running detached. This is not a leak +/// today because nothing `abort()`s these tasks — cancellation is cooperative via +/// `cancel_token`, checked inside `download_chunk_with_retry`. The extra task is +/// the price of capturing a `JoinError`; keep this invariant if adding `abort()`. +async fn join_download_task( + runtime_handle: &tokio::runtime::Handle, + fut: F, +) -> Result> +where + F: std::future::Future>> + Send + 'static, +{ + match runtime_handle.spawn(fut).await { + Ok(result) => result, + Err(join_err) => { + error!("CloudFetch download task panicked: {}", join_err); + Err(Error::io().message(format!("download task panicked: {}", join_err))) + } + } +} + impl StreamingCloudFetchProvider { /// Create a new provider. /// @@ -539,8 +571,20 @@ impl StreamingCloudFetchProvider { let chunks = self.chunks.clone(); let batch_merge_target_rows = self.config.batch_merge_target_rows; + // Run the download on its own task and AWAIT its JoinHandle here. + // tokio turns a panic inside a spawned task into a `JoinError` — but + // only if the handle is awaited. The prior fire-and-forget spawn dropped + // the handle, so a panic in the download/decompress path (e.g. the + // twox-hash overflow in LZ4 block-checksum verification — PECOBLR-4026) + // was silently swallowed, leaving the chunk stuck in `Downloading` + // forever and wedging the whole stream. Surfacing the panic as a + // download error lets the reader fail cleanly instead of hanging. + let rt = self.runtime_handle.clone(); self.runtime_handle.spawn(async move { - let result = Self::download_chunk_with_retry(chunk_index, &ctx).await; + let result = join_download_task(&rt, async move { + Self::download_chunk_with_retry(chunk_index, &ctx).await + }) + .await; match result { Ok(batches) => { @@ -1356,6 +1400,24 @@ mod tests { } } + /// Regression (PECOBLR-4026): a panicking download task must surface as an + /// error rather than being silently swallowed — a swallowed panic leaves the + /// chunk stuck `Downloading` and wedges the stream forever. Verifies + /// `join_download_task` converts a task panic into an error. + #[tokio::test] + async fn panicking_download_task_surfaces_as_error() { + let handle = tokio::runtime::Handle::current(); + let result = join_download_task(&handle, async { + panic!("simulated download-task panic"); + }) + .await; + let err = result.expect_err("a panicking download task must yield an error, not Ok"); + assert!( + err.to_string().contains("download task panicked"), + "error should identify a panicked download task, got: {err}" + ); + } + #[tokio::test] async fn persistent_download_failure_is_bounded_when_links_expire_in_flight() { use crate::client::retry::{build_retry_configs, RetryConfig};