arrowbricks 5.1.1__tar.gz → 5.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. arrowbricks-5.1.1/README.md → arrowbricks-5.1.2/PKG-INFO +15 -0
  2. arrowbricks-5.1.1/PKG-INFO → arrowbricks-5.1.2/README.md +2 -13
  3. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/pyproject.toml +1 -1
  4. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/Cargo.lock +1 -1
  5. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/Cargo.toml +1 -1
  6. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/sea.rs +39 -28
  7. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/thrift_exec.rs +66 -20
  8. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests/wiremock_pipeline.rs +113 -0
  9. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests/wiremock_thrift.rs +114 -0
  10. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/LICENSE +0 -0
  11. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/.gitignore +0 -0
  12. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/README.md +0 -0
  13. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/examples/duckdb_query.py +0 -0
  14. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/examples/fastapi_sse.py +0 -0
  15. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/proptest-regressions/json_convert.txt +0 -0
  16. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/rustfmt.toml +0 -0
  17. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/alloc.rs +0 -0
  18. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/arrow_ffi.rs +0 -0
  19. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/download.rs +0 -0
  20. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/error.rs +0 -0
  21. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/model.rs +0 -0
  22. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/thrift_rpc.rs +0 -0
  23. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client/volume.rs +0 -0
  24. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/client.rs +0 -0
  25. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/heartbeat.rs +0 -0
  26. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/json_convert.rs +0 -0
  27. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/lib.rs +0 -0
  28. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/ndjson.rs +0 -0
  29. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/reorder.rs +0 -0
  30. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/sea.rs +0 -0
  31. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/stats.rs +0 -0
  32. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline/test_support.rs +0 -0
  33. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/pipeline.rs +0 -0
  34. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/src/thrift.rs +0 -0
  35. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests/common/mod.rs +0 -0
  36. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests/wiremock_volume_files.rs +0 -0
  37. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/conftest.py +0 -0
  38. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_ipc_stream.py +0 -0
  39. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_parameters.py +0 -0
  40. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_stream_ndjson_lines.py +0 -0
  41. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_streaming.py +0 -0
  42. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_thrift.py +0 -0
  43. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_token_provider.py +0 -0
  44. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/test_volume_files.py +0 -0
  45. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/rust/arrowbricks_core/tests_py/thrift_mock.py +0 -0
  46. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/__init__.py +0 -0
  47. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/_core.pyi +0 -0
  48. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/_streaming.py +0 -0
  49. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/client.py +0 -0
  50. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/cursor.py +0 -0
  51. {arrowbricks-5.1.1 → arrowbricks-5.1.2}/src/arrowbricks/py.typed +0 -0
@@ -1,3 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: arrowbricks
3
+ Version: 5.1.2
4
+ Requires-Dist: arro3-core>=0.8 ; extra == 'arro3'
5
+ Provides-Extra: arro3
6
+ License-File: LICENSE
7
+ Summary: Runs SQL against a Databricks SQL warehouse via the Statement Execution API and hands you the result as Arrow -- a DB-API-ish Cursor (fetchone/fetchmany/fetchall/fetchall_arrow) or NDJSON streaming. Rust/PyO3 core throughout -- zero required runtime dependencies.
8
+ License-Expression: MIT
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
11
+ Project-URL: Repository, https://github.com/bmsuisse/arrowbricks
12
+
1
13
  <p align="center">
2
14
  <img src="assets/logo.svg" width="72" height="72" alt="arrowbricks logo">
3
15
  </p>
@@ -152,6 +164,8 @@ When `total_timeout_s` elapses or your code cancels the surrounding coroutine (`
152
164
 
153
165
  This covers both phases of a query: the submit/poll wait while the statement is still running (`Cursor.execute()`/`execute_streamed()`, and the start of `client.stream_query_json(...)`), and the chunk download afterwards (`Cursor.fetchall_streamed()`/`fetchall_arrow_streamed()`, the rest of `stream_query_json`). `stream_query_json`'s `total_timeout_s` is one budget across both phases. On `protocol="sea"` there is one short blind spot: the submit request itself can wait server-side for up to `wait_timeout` (default 30s) before Databricks returns a statement id, and a query abandoned inside that window can't be cancelled.
154
166
 
167
+ Abandoning a result also stops the client-side work right away, for every fetch method (plain `fetchall_arrow()`/`fetchmany()` too): cancelling the task, closing a `stream_query_json` iterator early (what a web framework does when the HTTP client disconnects mid-response), or dropping a `Cursor` before its result is drained aborts the chunk downloads in flight, starts no new ones, and on Thrift closes the operation (`CloseOperation`). Nothing to opt into: cancel the task (or let your framework close the generator) and arrowbricks cleans up. Cancelling a FastAPI handler on client disconnect therefore stops the warehouse query (if it is still running) and the downloads (if it is not).
168
+
155
169
  ## Errors
156
170
 
157
171
  Every exception arrowbricks raises itself is an `ArrowbricksError` -- a `RuntimeError` subclass, so an `except RuntimeError` written before this hierarchy existed keeps catching everything unchanged. Three subclasses split by what actually changes what your code should do next:
@@ -332,3 +346,4 @@ can run concurrently on separate Python threads.
332
346
  ## License
333
347
 
334
348
  MIT
349
+
@@ -1,15 +1,3 @@
1
- Metadata-Version: 2.4
2
- Name: arrowbricks
3
- Version: 5.1.1
4
- Requires-Dist: arro3-core>=0.8 ; extra == 'arro3'
5
- Provides-Extra: arro3
6
- License-File: LICENSE
7
- Summary: Runs SQL against a Databricks SQL warehouse via the Statement Execution API and hands you the result as Arrow -- a DB-API-ish Cursor (fetchone/fetchmany/fetchall/fetchall_arrow) or NDJSON streaming. Rust/PyO3 core throughout -- zero required runtime dependencies.
8
- License-Expression: MIT
9
- Requires-Python: >=3.11
10
- Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
11
- Project-URL: Repository, https://github.com/bmsuisse/arrowbricks
12
-
13
1
  <p align="center">
14
2
  <img src="assets/logo.svg" width="72" height="72" alt="arrowbricks logo">
15
3
  </p>
@@ -164,6 +152,8 @@ When `total_timeout_s` elapses or your code cancels the surrounding coroutine (`
164
152
 
165
153
  This covers both phases of a query: the submit/poll wait while the statement is still running (`Cursor.execute()`/`execute_streamed()`, and the start of `client.stream_query_json(...)`), and the chunk download afterwards (`Cursor.fetchall_streamed()`/`fetchall_arrow_streamed()`, the rest of `stream_query_json`). `stream_query_json`'s `total_timeout_s` is one budget across both phases. On `protocol="sea"` there is one short blind spot: the submit request itself can wait server-side for up to `wait_timeout` (default 30s) before Databricks returns a statement id, and a query abandoned inside that window can't be cancelled.
166
154
 
155
+ Abandoning a result also stops the client-side work right away, for every fetch method (plain `fetchall_arrow()`/`fetchmany()` too): cancelling the task, closing a `stream_query_json` iterator early (what a web framework does when the HTTP client disconnects mid-response), or dropping a `Cursor` before its result is drained aborts the chunk downloads in flight, starts no new ones, and on Thrift closes the operation (`CloseOperation`). Nothing to opt into: cancel the task (or let your framework close the generator) and arrowbricks cleans up. Cancelling a FastAPI handler on client disconnect therefore stops the warehouse query (if it is still running) and the downloads (if it is not).
156
+
167
157
  ## Errors
168
158
 
169
159
  Every exception arrowbricks raises itself is an `ArrowbricksError` -- a `RuntimeError` subclass, so an `except RuntimeError` written before this hierarchy existed keeps catching everything unchanged. Three subclasses split by what actually changes what your code should do next:
@@ -344,4 +334,3 @@ can run concurrently on separate Python threads.
344
334
  ## License
345
335
 
346
336
  MIT
347
-
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "arrowbricks"
3
- version = "5.1.1"
3
+ version = "5.1.2"
4
4
  description = "Runs SQL against a Databricks SQL warehouse via the Statement Execution API and hands you the result as Arrow -- a DB-API-ish Cursor (fetchone/fetchmany/fetchall/fetchall_arrow) or NDJSON streaming. Rust/PyO3 core throughout -- zero required runtime dependencies."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -177,7 +177,7 @@ dependencies = [
177
177
 
178
178
  [[package]]
179
179
  name = "arrowbricks_core"
180
- version = "5.1.1"
180
+ version = "5.1.2"
181
181
  dependencies = [
182
182
  "arrow-array",
183
183
  "arrow-buffer",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "arrowbricks_core"
3
- version = "5.1.1"
3
+ version = "5.1.2"
4
4
  edition = "2024"
5
5
  readme = "README.md"
6
6
 
@@ -557,38 +557,49 @@ impl DbClient {
557
557
  let statement_id = statement_id.clone();
558
558
  let stats = stats.clone();
559
559
  handles.push(tokio::spawn(async move {
560
- loop {
561
- let meta = { queue.lock().unwrap().pop_front() };
562
- let Some(meta) = meta else { return Ok(()) };
563
- let fetched = if meta.pre_resolved_links.is_empty() {
564
- client
565
- .fetch_chunk_index(&statement_id, meta.chunk_index, compressed, &stats)
566
- .await
567
- } else {
568
- client
569
- .fetch_pre_resolved_links(&meta.pre_resolved_links, compressed, &stats)
570
- .await
571
- };
572
- match fetched {
573
- Ok(blobs) => {
574
- // One blob ⇒ `meta.row_count` (the whole chunk_index's
575
- // declared count) and "this blob's count" are the same
576
- // number -- see `ChunkItem::truncate_to`'s own doc comment.
577
- let truncate_to = if blobs.len() == 1 { meta.row_count } else { None };
578
- for blob in blobs {
579
- let item = ChunkItem {
580
- blob,
581
- row_count: meta.row_count,
582
- chunk_index: meta.chunk_index,
583
- truncate_to,
584
- };
585
- if worker_tx.send(Ok(item)).await.is_err() {
586
- return Ok(());
560
+ let work_loop = async {
561
+ loop {
562
+ let meta = { queue.lock().unwrap().pop_front() };
563
+ let Some(meta) = meta else { return Ok(()) };
564
+ let fetched = if meta.pre_resolved_links.is_empty() {
565
+ client
566
+ .fetch_chunk_index(&statement_id, meta.chunk_index, compressed, &stats)
567
+ .await
568
+ } else {
569
+ client
570
+ .fetch_pre_resolved_links(&meta.pre_resolved_links, compressed, &stats)
571
+ .await
572
+ };
573
+ match fetched {
574
+ Ok(blobs) => {
575
+ // One blob ⇒ `meta.row_count` (the whole chunk_index's
576
+ // declared count) and "this blob's count" are the same
577
+ // number -- see `ChunkItem::truncate_to`'s own doc comment.
578
+ let truncate_to = if blobs.len() == 1 { meta.row_count } else { None };
579
+ for blob in blobs {
580
+ let item = ChunkItem {
581
+ blob,
582
+ row_count: meta.row_count,
583
+ chunk_index: meta.chunk_index,
584
+ truncate_to,
585
+ };
586
+ if worker_tx.send(Ok(item)).await.is_err() {
587
+ return Ok(());
588
+ }
587
589
  }
588
590
  }
591
+ Err(e) => return Err(e),
589
592
  }
590
- Err(e) => return Err(e),
591
593
  }
594
+ };
595
+ // The consumer dropping its receiver (a cancelled,
596
+ // timed-out or abandoned fetch) stops this worker at
597
+ // once, aborting its in-flight download instead of
598
+ // finishing -- and then starting -- chunks nobody reads.
599
+ tokio::select! {
600
+ biased;
601
+ () = worker_tx.closed() => Ok(()),
602
+ res = work_loop => res,
592
603
  }
593
604
  }));
594
605
  }
@@ -626,17 +626,35 @@ async fn run_thrift_fetch_loop(
626
626
  let compressed_flag = compressed_flag.clone();
627
627
  let worker_stats = stats.clone();
628
628
  worker_handles.push(tokio::spawn(async move {
629
- loop {
630
- let work = { link_rx.lock().await.recv().await };
631
- let Some(work) = work else { return };
632
- let compressed = compressed_flag.load(std::sync::atomic::Ordering::Relaxed);
633
- let result = fetch_thrift_link(&client, work, compressed, &worker_stats).await;
634
- if out_tx.send(result).await.is_err() {
635
- return;
629
+ let work_loop = async {
630
+ loop {
631
+ let work = { link_rx.lock().await.recv().await };
632
+ let Some(work) = work else { return };
633
+ let compressed = compressed_flag.load(std::sync::atomic::Ordering::Relaxed);
634
+ let result = fetch_thrift_link(&client, work, compressed, &worker_stats).await;
635
+ if out_tx.send(result).await.is_err() {
636
+ return;
637
+ }
636
638
  }
639
+ };
640
+ // The consumer (`ResultStream`/`NdjsonStream`) dropping its
641
+ // receiver -- a cancelled or abandoned fetch -- stops this worker
642
+ // at once, aborting its in-flight download, instead of finishing
643
+ // a chunk nobody will read. See `queue_link`.
644
+ tokio::select! {
645
+ biased;
646
+ () = out_tx.closed() => {}
647
+ () = work_loop => {}
637
648
  }
638
649
  }));
639
650
  }
651
+ // Only the workers hold the receiving end from here on: if every worker
652
+ // has exited, `link_tx.send` fails instead of waiting forever for room in
653
+ // a queue nobody drains -- which used to hang this task (and skip the
654
+ // `CloseOperation`/`CloseSession` cleanup in `drive_thrift_fetch_loop`)
655
+ // whenever a consumer abandoned a result with more than about
656
+ // `2 * concurrency` links left to queue.
657
+ drop(link_rx);
640
658
 
641
659
  let mut chunk_index: i64 = 0;
642
660
  let mut pending = initial_rowset;
@@ -658,11 +676,20 @@ async fn run_thrift_fetch_loop(
658
676
  // Direct results may already have confirmed it during submission. In
659
677
  // that case downloads can overlap the very first FetchResults RPC too.
660
678
  let mut pending_until_confirmed: Vec<ThriftLinkWork> = Vec::new();
661
- loop {
679
+ 'discover: loop {
662
680
  let (row_set, has_more) = if let Some(v) = pending.take() {
663
681
  v
664
682
  } else {
665
- match client.thrift_fetch_results_raw(operation, stats).await {
683
+ // Stop discovering as soon as the consumer is gone (see
684
+ // `queue_link`) -- no further `FetchResults` round trips for a
685
+ // result nobody reads; `drive_thrift_fetch_loop` then closes the
686
+ // operation.
687
+ let fetched = tokio::select! {
688
+ biased;
689
+ () = tx.closed() => break 'discover,
690
+ fetched = client.thrift_fetch_results_raw(operation, stats) => fetched,
691
+ };
692
+ match fetched {
666
693
  Ok(fr) => {
667
694
  if let Some(e) = fr.status.error() {
668
695
  let _ = tx
@@ -752,14 +779,11 @@ async fn run_thrift_fetch_loop(
752
779
  // every worker is currently busy, so this await is exactly
753
780
  // the same "peak buffered stays at ~concurrency" trade-off
754
781
  // `fetch_chunks_with_backpressure`'s own doc comment
755
- // describes. The receiving end only ever closes once every
756
- // worker returns, which only happens after this sender side
757
- // is dropped -- so a closed-channel send here would mean
758
- // every worker already exited (e.g. all panicked), not a
759
- // normal condition; still handled without panicking
760
- // regardless.
761
- if link_tx.send(work).await.is_err() {
762
- break;
782
+ // describes. `false` means the consumer is gone or every
783
+ // worker already exited (e.g. all panicked) -- stop
784
+ // discovering either way, see `queue_link`.
785
+ if !queue_link(&link_tx, work, tx).await {
786
+ break 'discover;
763
787
  }
764
788
  } else {
765
789
  // See this function's own comment above `metadata_confirmed`.
@@ -769,8 +793,8 @@ async fn run_thrift_fetch_loop(
769
793
 
770
794
  if metadata_confirmed {
771
795
  for work in pending_until_confirmed.drain(..) {
772
- if link_tx.send(work).await.is_err() {
773
- break;
796
+ if !queue_link(&link_tx, work, tx).await {
797
+ break 'discover;
774
798
  }
775
799
  }
776
800
  }
@@ -783,7 +807,7 @@ async fn run_thrift_fetch_loop(
783
807
  // Final flush for the never-confirmed case -- see `metadata_confirmed`'s
784
808
  // own comment above.
785
809
  for work in pending_until_confirmed.drain(..) {
786
- if link_tx.send(work).await.is_err() {
810
+ if !queue_link(&link_tx, work, tx).await {
787
811
  break;
788
812
  }
789
813
  }
@@ -794,6 +818,28 @@ async fn run_thrift_fetch_loop(
794
818
  }
795
819
  }
796
820
 
821
+ /// Hands one discovered link to the download workers, unless the consumer
822
+ /// has dropped its end of `out` (a cancelled, timed-out or abandoned
823
+ /// `ResultStream`/`NdjsonStream`) first. Returns `false` when discovery
824
+ /// should stop: the consumer is gone, or every worker has already exited.
825
+ ///
826
+ /// Without the `out.closed()` arm, an abandoned result kept this producer
827
+ /// issuing `FetchResults` and queueing links for the whole statement, and --
828
+ /// once the workers had noticed the closed channel and exited -- blocked
829
+ /// forever on a full link queue, so `drive_thrift_fetch_loop` never reached
830
+ /// `CloseOperation`/`CloseSession` and its task leaked.
831
+ async fn queue_link(
832
+ link_tx: &mpsc::Sender<ThriftLinkWork>,
833
+ work: ThriftLinkWork,
834
+ out: &mpsc::Sender<Result<ChunkItem, ApiError>>,
835
+ ) -> bool {
836
+ tokio::select! {
837
+ biased;
838
+ () = out.closed() => false,
839
+ sent = link_tx.send(work) => sent.is_ok(),
840
+ }
841
+ }
842
+
797
843
  /// A panicked download worker has already pulled its `ThriftLinkWork` off
798
844
  /// the queue and sends nothing on `out_tx` -- silently discarding that (a
799
845
  /// bare `let _ = handle.await`) would mean the channel closing normally
@@ -1755,3 +1755,116 @@ async fn abandoning_the_submit_poll_wait_releases_the_session_reservation() {
1755
1755
  "every abandoned attempt must release its reservation, so each one can create a session"
1756
1756
  );
1757
1757
  }
1758
+
1759
+ /// A raw cloud-fetch blob server for `sea_dropping_a_partly_read_result_aborts_in_flight_downloads`:
1760
+ /// `/blob-0` is served in full; any other path gets its headers and first
1761
+ /// few bytes, then the connection is held open until the *client* hangs
1762
+ /// up, which is signalled on `hung_up` (`started` fires once such a stalled
1763
+ /// download is in flight). wiremock can't show a client abandoning a
1764
+ /// response mid-body, hence a bare socket, same as `client/download.rs`'s own
1765
+ /// truncated-response tests.
1766
+ async fn spawn_stalling_blob_server(body: Vec<u8>) -> (String, Arc<tokio::sync::Notify>, Arc<tokio::sync::Notify>) {
1767
+ use tokio::io::{AsyncReadExt, AsyncWriteExt};
1768
+
1769
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1770
+ let base = format!("http://{}", listener.local_addr().unwrap());
1771
+ let started = Arc::new(tokio::sync::Notify::new());
1772
+ let hung_up = Arc::new(tokio::sync::Notify::new());
1773
+ let (started_srv, hung_up_srv) = (started.clone(), hung_up.clone());
1774
+ tokio::spawn(async move {
1775
+ loop {
1776
+ let Ok((mut socket, _)) = listener.accept().await else {
1777
+ return;
1778
+ };
1779
+ let body = body.clone();
1780
+ let (started, hung_up) = (started_srv.clone(), hung_up_srv.clone());
1781
+ tokio::spawn(async move {
1782
+ let mut request = Vec::new();
1783
+ while !request.ends_with(b"\r\n\r\n") {
1784
+ match socket.read_u8().await {
1785
+ Ok(b) => request.push(b),
1786
+ Err(_) => return,
1787
+ }
1788
+ }
1789
+ let head = format!(
1790
+ "HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
1791
+ body.len()
1792
+ );
1793
+ socket.write_all(head.as_bytes()).await.unwrap();
1794
+ if request.starts_with(b"GET /blob-0 ") {
1795
+ socket.write_all(&body).await.unwrap();
1796
+ let _ = socket.shutdown().await;
1797
+ return;
1798
+ }
1799
+ socket.write_all(&body[..10]).await.unwrap();
1800
+ started.notify_one();
1801
+ let mut buf = [0u8; 64];
1802
+ loop {
1803
+ match socket.read(&mut buf).await {
1804
+ Ok(0) | Err(_) => break,
1805
+ Ok(_) => {}
1806
+ }
1807
+ }
1808
+ hung_up.notify_one();
1809
+ });
1810
+ }
1811
+ });
1812
+ (base, started, hung_up)
1813
+ }
1814
+
1815
+ /// Dropping a partly-read `ResultStream` (a cancelled or abandoned fetch)
1816
+ /// must abort the chunk download already in flight, not let it run to
1817
+ /// completion -- here that would mean waiting out the full `http_timeout`
1818
+ /// on a download that never finishes. Before this was fixed the worker
1819
+ /// only noticed the dropped consumer when it tried to hand over a finished
1820
+ /// chunk.
1821
+ #[tokio::test]
1822
+ async fn sea_dropping_a_partly_read_result_aborts_in_flight_downloads() {
1823
+ let server = MockServer::start().await;
1824
+ mount_warehouse_running(&server).await;
1825
+ let (blob_base, started, hung_up) = spawn_stalling_blob_server(build_chunk_bytes(0, 5)).await;
1826
+ let chunks: Vec<_> = (0..3).map(|i| json!({"chunk_index": i, "row_count": 5})).collect();
1827
+ Mock::given(method("POST"))
1828
+ .and(path("/api/2.0/sql/statements"))
1829
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
1830
+ "statement_id": STATEMENT_ID,
1831
+ "status": {"state": "SUCCEEDED"},
1832
+ "manifest": {
1833
+ "chunks": chunks,
1834
+ "schema": {"columns": [
1835
+ {"name": "id", "type_name": "LONG"},
1836
+ {"name": "label", "type_name": "STRING"},
1837
+ ]},
1838
+ },
1839
+ })))
1840
+ .mount(&server)
1841
+ .await;
1842
+ for i in 0..3 {
1843
+ Mock::given(method("GET"))
1844
+ .and(path(format!(
1845
+ "/api/2.0/sql/statements/{STATEMENT_ID}/result/chunks/{i}"
1846
+ )))
1847
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
1848
+ "external_links": [{"external_link": format!("{blob_base}/blob-{i}")}]
1849
+ })))
1850
+ .mount(&server)
1851
+ .await;
1852
+ }
1853
+
1854
+ let client = Arc::new(
1855
+ DbClient::new(&server.uri(), WAREHOUSE_ID, "fake-token")
1856
+ .with_protocol(Protocol::Sea)
1857
+ .with_concurrency(1),
1858
+ );
1859
+ let mut stream = execute_lazy(client, "SELECT * FROM t", None, None, None).await.unwrap();
1860
+ let (batches, _schema) = stream.fetchmany_arrow(1).await.unwrap();
1861
+ assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 1);
1862
+ tokio::time::timeout(std::time::Duration::from_secs(5), started.notified())
1863
+ .await
1864
+ .expect("the next chunk's download must be in flight before the drop");
1865
+
1866
+ drop(stream);
1867
+ tokio::time::timeout(std::time::Duration::from_secs(2), hung_up.notified())
1868
+ .await
1869
+ .expect("dropping the result must abort the in-flight download promptly");
1870
+ }
@@ -2068,3 +2068,117 @@ async fn thrift_abandoning_the_submit_poll_wait_closes_its_session() {
2068
2068
  wait_for_calls(&close_calls, 1).await;
2069
2069
  assert_eq!(close_calls.load(Ordering::SeqCst), 1);
2070
2070
  }
2071
+
2072
+ /// Abandoning a partly-read result -- the consumer drops its `ResultStream`
2073
+ /// after the first chunk (a cancelled `fetchall`, a client that stopped
2074
+ /// reading a stream) -- must stop the background fetch loop and close the
2075
+ /// operation promptly. It used to keep the remaining links queued: once the
2076
+ /// one worker noticed the closed channel and exited, the producer blocked
2077
+ /// forever on a full link queue, so `CloseOperation` never went out (and
2078
+ /// the in-flight download ran to completion first in any case). The slow
2079
+ /// links here take 5s, so a `CloseOperation` within `wait_for_calls`' ~1s
2080
+ /// also proves the in-flight download was aborted rather than finished.
2081
+ #[tokio::test]
2082
+ async fn thrift_dropping_a_partly_read_result_aborts_downloads_and_closes_the_operation() {
2083
+ let server = MockServer::start().await;
2084
+ mount_open_session_always(&server, b"sess-abandon").await;
2085
+ Mock::given(method("POST"))
2086
+ .and(path(thrift_path()))
2087
+ .and(IsThriftRpc("ExecuteStatement"))
2088
+ .respond_with(ResponseTemplate::new(200).set_body_raw(
2089
+ build_execute_statement_resp(b"op-abandon", b"opsecret-abandon", None),
2090
+ "application/x-thrift",
2091
+ ))
2092
+ .mount(&server)
2093
+ .await;
2094
+ Mock::given(method("POST"))
2095
+ .and(path(thrift_path()))
2096
+ .and(IsThriftRpc("GetOperationStatus"))
2097
+ .respond_with(ResponseTemplate::new(200).set_body_raw(
2098
+ build_get_operation_status_resp(operation_state::FINISHED, None),
2099
+ "application/x-thrift",
2100
+ ))
2101
+ .mount(&server)
2102
+ .await;
2103
+ let cancel_calls = mount_cancel_operation_ok(&server).await;
2104
+ let close_calls = Arc::new(AtomicUsize::new(0));
2105
+ let close_calls_for_mock = close_calls.clone();
2106
+ Mock::given(method("POST"))
2107
+ .and(path(thrift_path()))
2108
+ .and(IsThriftRpc("CloseOperation"))
2109
+ .respond_with(move |_req: &Request| {
2110
+ close_calls_for_mock.fetch_add(1, Ordering::SeqCst);
2111
+ ResponseTemplate::new(200).set_body_raw(build_close_operation_resp(), "application/x-thrift")
2112
+ })
2113
+ .mount(&server)
2114
+ .await;
2115
+
2116
+ // More links than one worker plus its one-slot queue can hold.
2117
+ const N_LINKS: i64 = 8;
2118
+ const ROWS_PER_CHUNK: i64 = 5;
2119
+ let uri = server.uri();
2120
+ let links: Vec<(String, i64)> = (0..N_LINKS)
2121
+ .map(|i| (format!("{uri}/_data/abandon-{i}"), ROWS_PER_CHUNK))
2122
+ .collect();
2123
+ Mock::given(method("POST"))
2124
+ .and(path(thrift_path()))
2125
+ .and(IsThriftRpc("FetchResults"))
2126
+ .respond_with(ResponseTemplate::new(200).set_body_raw(
2127
+ build_fetch_results_resp(&FetchSpec {
2128
+ has_more_rows: false,
2129
+ result_links: links,
2130
+ metadata: Some((false, None)),
2131
+ ..Default::default()
2132
+ }),
2133
+ "application/x-thrift",
2134
+ ))
2135
+ .mount(&server)
2136
+ .await;
2137
+ let schema = test_schema();
2138
+ let data_calls = Arc::new(AtomicUsize::new(0));
2139
+ for i in 0..N_LINKS {
2140
+ let bytes = build_full_stream_bytes(&schema, i * ROWS_PER_CHUNK, (i + 1) * ROWS_PER_CHUNK);
2141
+ let delay = std::time::Duration::from_millis(if i == 0 { 0 } else { 5_000 });
2142
+ let data_calls_for_mock = data_calls.clone();
2143
+ Mock::given(method("GET"))
2144
+ .and(path(format!("/_data/abandon-{i}")))
2145
+ .respond_with(move |_req: &Request| {
2146
+ data_calls_for_mock.fetch_add(1, Ordering::SeqCst);
2147
+ ResponseTemplate::new(200)
2148
+ .set_body_raw(bytes.clone(), "application/vnd.apache.arrow.stream")
2149
+ .set_delay(delay)
2150
+ })
2151
+ .mount(&server)
2152
+ .await;
2153
+ }
2154
+
2155
+ let client = Arc::new(
2156
+ DbClient::new(&server.uri(), WAREHOUSE_ID, "fake-token")
2157
+ .with_protocol(Protocol::Thrift)
2158
+ .with_concurrency(1),
2159
+ );
2160
+ let mut stream = execute_lazy_thrift(client, "SELECT * FROM t", None, None, None)
2161
+ .await
2162
+ .unwrap();
2163
+ let (batches, _schema) = stream.fetchmany_arrow(1).await.unwrap();
2164
+ assert_eq!(batches.iter().map(|b| b.num_rows()).sum::<usize>(), 1);
2165
+ wait_for_calls(&data_calls, 2).await; // the second (slow) download is in flight
2166
+ drop(stream);
2167
+
2168
+ wait_for_calls(&close_calls, 1).await;
2169
+ assert_eq!(
2170
+ close_calls.load(Ordering::SeqCst),
2171
+ 1,
2172
+ "CloseOperation must go out promptly once the consumer drops a partly-read result"
2173
+ );
2174
+ assert_eq!(
2175
+ data_calls.load(Ordering::SeqCst),
2176
+ 2,
2177
+ "no further download may start after the consumer is gone"
2178
+ );
2179
+ assert_eq!(
2180
+ cancel_calls.load(Ordering::SeqCst),
2181
+ 0,
2182
+ "a finished operation is closed, not cancelled"
2183
+ );
2184
+ }
File without changes