@@ -42,7 +42,7 @@ use arrow_ipc::reader::StreamReader;
4242use arrow_schema:: { ArrowError , Field , Schema , SchemaRef } ;
4343use async_trait:: async_trait;
4444use std:: io:: Cursor ;
45- use std:: sync:: Arc ;
45+ use std:: sync:: { Arc , Weak } ;
4646use std:: time:: Duration ;
4747
4848pub use cloudfetch:: StreamingCloudFetchProvider as CloudFetchReader ;
@@ -54,7 +54,10 @@ pub use cloudfetch::StreamingCloudFetchProvider as CloudFetchReader;
5454/// or any future backend implementation.
5555#[ derive( Debug ) ]
5656pub ( crate ) struct ResultReaderFactory {
57- client : Arc < dyn Transport > ,
57+ /// Weak because the transport owns this factory: a strong reference
58+ /// would form a cycle that keeps the transport, its HTTP client and its
59+ /// runtime handle alive after the session closes.
60+ client : Weak < dyn Transport > ,
5861 http_client : Arc < DatabricksHttpClient > ,
5962 config : CloudFetchConfig ,
6063 post_process_options : PostProcessOptions ,
@@ -64,7 +67,7 @@ pub(crate) struct ResultReaderFactory {
6467impl ResultReaderFactory {
6568 /// Create a new result reader factory.
6669 pub ( crate ) fn new (
67- client : Arc < dyn Transport > ,
70+ client : Weak < dyn Transport > ,
6871 http_client : Arc < DatabricksHttpClient > ,
6972 config : CloudFetchConfig ,
7073 mut post_process_options : PostProcessOptions ,
@@ -81,6 +84,15 @@ impl ResultReaderFactory {
8184 }
8285 }
8386
87+ /// Readers hold a strong reference so an open result stream keeps the
88+ /// transport alive for its own chunk fetches.
89+ fn transport ( & self ) -> Result < Arc < dyn Transport > > {
90+ self . client . upgrade ( ) . ok_or_else ( || {
91+ Error :: invalid_state ( )
92+ . message ( "transport was dropped before the result reader was created" )
93+ } )
94+ }
95+
8496 /// Create a reader from a statement execution response.
8597 ///
8698 /// Automatically selects CloudFetch, inline, or other reader based
@@ -546,7 +558,7 @@ impl ResultReaderFactory {
546558 // out the per-link field yet — see
547559 // `SeaChunkLinkFetcher::decode_continuation`.
548560 let fetcher = SeaChunkLinkFetcher :: new (
549- self . client . clone ( ) ,
561+ self . transport ( ) ? ,
550562 statement_id. to_string ( ) ,
551563 external_links,
552564 total_chunk_count,
@@ -628,7 +640,7 @@ impl ResultReaderFactory {
628640
629641 let source: Arc < dyn crate :: reader:: inline:: InlineChunkSource > =
630642 Arc :: new ( AttributedInlineChunkSource {
631- client : Arc :: clone ( & self . client ) ,
643+ client : self . transport ( ) ? ,
632644 session_attribution,
633645 } ) ;
634646 let provider = InlineStreamingProvider :: new (
@@ -2103,20 +2115,23 @@ mod tests {
21032115
21042116 /// Build a `ResultReaderFactory` against the panicking stub
21052117 /// transport. Used by control-flow tests that don't fire any
2106- /// RPC.
2107- fn factory_for_control_flow_test ( ) -> ResultReaderFactory {
2118+ /// RPC. The factory only holds the transport weakly, so callers
2119+ /// must keep the returned transport alive for the test.
2120+ fn factory_for_control_flow_test ( ) -> ( ResultReaderFactory , Arc < dyn Transport > ) {
21082121 let http_client = Arc :: new (
21092122 DatabricksHttpClient :: with_default_retry ( HttpClientConfig :: default ( ) ) . unwrap ( ) ,
21102123 ) ;
21112124 let runtime = tokio:: runtime:: Handle :: current ( ) ;
2112- ResultReaderFactory :: new (
2113- Arc :: new ( PanickingTransport ) ,
2125+ let transport: Arc < dyn Transport > = Arc :: new ( PanickingTransport ) ;
2126+ let factory = ResultReaderFactory :: new (
2127+ Arc :: downgrade ( & transport) ,
21142128 http_client,
21152129 CloudFetchConfig :: default ( ) ,
21162130 PostProcessOptions :: default ( ) ,
21172131 GeospatialResultRepresentation :: String ,
21182132 runtime,
2119- )
2133+ ) ;
2134+ ( factory, transport)
21202135 }
21212136
21222137 fn ok_status ( ) -> StatementStatus {
@@ -2157,7 +2172,7 @@ mod tests {
21572172 /// result after the first chunk.
21582173 #[ tokio:: test]
21592174 async fn rollout_guard_errors_when_total_chunks_disagrees_with_next_chunk_index ( ) {
2160- let factory = factory_for_control_flow_test ( ) ;
2175+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
21612176 let response = ExecuteResponse {
21622177 statement_id : "stmt-1" . to_string ( ) ,
21632178 status : ok_status ( ) ,
@@ -2197,7 +2212,7 @@ mod tests {
21972212 /// `total_chunk_count`.
21982213 #[ tokio:: test]
21992214 async fn rollout_guard_does_not_fire_on_genuinely_single_chunk ( ) {
2200- let factory = factory_for_control_flow_test ( ) ;
2215+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
22012216 let response = ExecuteResponse {
22022217 statement_id : "stmt-1" . to_string ( ) ,
22032218 status : ok_status ( ) ,
@@ -2234,7 +2249,7 @@ mod tests {
22342249 /// to 0 would silently corrupt the next-fetch row_offset.
22352250 #[ tokio:: test]
22362251 async fn initial_response_requires_row_count_when_streaming ( ) {
2237- let factory = factory_for_control_flow_test ( ) ;
2252+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
22382253 let response = ExecuteResponse {
22392254 statement_id : "stmt-1" . to_string ( ) ,
22402255 status : ok_status ( ) ,
@@ -2268,7 +2283,7 @@ mod tests {
22682283
22692284 #[ tokio:: test]
22702285 async fn initial_response_requires_chunk_index_when_streaming ( ) {
2271- let factory = factory_for_control_flow_test ( ) ;
2286+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
22722287 let response = ExecuteResponse {
22732288 statement_id : "stmt-1" . to_string ( ) ,
22742289 status : ok_status ( ) ,
@@ -2315,7 +2330,7 @@ mod tests {
23152330 /// rather than a silent pass.
23162331 #[ tokio:: test]
23172332 async fn zero_chunk_count_short_circuits_to_empty_reader ( ) {
2318- let factory = factory_for_control_flow_test ( ) ;
2333+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
23192334 let response = ExecuteResponse {
23202335 statement_id : "stmt-1" . to_string ( ) ,
23212336 status : ok_status ( ) ,
@@ -2348,7 +2363,7 @@ mod tests {
23482363 /// of the field is not mis-read as `Some(0)`.
23492364 #[ tokio:: test]
23502365 async fn absent_total_chunk_count_falls_through ( ) {
2351- let factory = factory_for_control_flow_test ( ) ;
2366+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
23522367 let response = ExecuteResponse {
23532368 statement_id : "stmt-1" . to_string ( ) ,
23542369 status : ok_status ( ) ,
@@ -2372,7 +2387,7 @@ mod tests {
23722387 /// it's not the manifest-driven `EmptyReader` path.
23732388 #[ tokio:: test]
23742389 async fn non_zero_total_chunk_count_does_not_short_circuit ( ) {
2375- let factory = factory_for_control_flow_test ( ) ;
2390+ let ( factory, _transport ) = factory_for_control_flow_test ( ) ;
23762391 let response = ExecuteResponse {
23772392 statement_id : "stmt-1" . to_string ( ) ,
23782393 status : ok_status ( ) ,
0 commit comments