Skip to content

fix(sea): close orphaned statement when execute_and_read fails - #92

Merged
vikrantpuppala merged 4 commits into
mainfrom
vikrant/cloudfetch-close-on-create-reader-failure
Jun 1, 2026
Merged

vikrantpuppala merged 4 commits into
mainfrom
vikrant/cloudfetch-close-on-create-reader-failure

Conversation

@vikrantpuppala

@vikrantpuppala vikrantpuppala commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

After call_execute_api succeeds the SEA statement is open on the server. Two paths into a leak that would otherwise persist until SQL Gateway's CommandInactivityTimeout (~1h):

  1. wait_for_completion returns Error::timeout while the server still considers the statement live in PENDING / RUNNING. Terminal FAILED / CANCELED is the server's own cleanup signal and doesn't need a CloseStatement, but the polling-timeout path does.
  2. create_reader fails. The most likely sub-failure today is ResultPostProcessor::new → StreamingCloudFetchProvider::get_schema eagerly downloading chunk 0 to extract the result schema; a non-retriable 4xx / persistent 5xx on that GET propagates up.

The kernel never issues DELETE /api/2.0/sql/statements/{id} in either case.

How the bug surfaced

Investigation triggered by databricks-driver-test#410 CLOUDFETCH-016. The proxy-test suite ran a perpetual cloudfetch_500 scenario against SELECT id FROM range(2000000) and observed:

[baseline] raised=1 CloseStatement=0 DeleteSession=1

Session-level cleanup fires, but the per-statement handle is orphaned. Traced down to execute_and_read: it propagates create_reader errors via ? without persisting the statement_id for later cleanup.

Fix

Capture the statement_id from the initial response (before wait_for_completion) and fire a best-effort close on either failure path via the Transport::close_statement trait method. close_statement already swallows its own errors; the wrapper here is a logging seam that attributes which phase failed (wait_for_completion vs create_reader) so traces stay diagnosable.

Validation

Unit reproducer (tests/v0_execute_wiremock.rs::execute_cloudfetch_chunk0_failure_closes_statement): mounts a SUCCEEDED execute response with external_links pointing at the mock server, then 404s the chunk-0 GET. Asserts the mock observes a DELETE /api/2.0/sql/statements/{id}.

  • Baseline (before this PR): FAIL — 5 chunk-0 GET retries then no DELETE.
  • With fix: PASS in 6.03s.

Full suite: cargo test --workspace --lib --tests — 619 tests pass, 0 failures, 0 new ignored. cargo clippy --all-targets clean (no new warnings from the modified files; pre-existing fmt drift in src/reader/cloudfetch/arrow_parser.rs is unrelated and pre-existing on main).

End-to-end via driver-test: rebuilt the proxy-test venv against this branch (DATABRICKS_KERNEL_PATH=$BRANCH) and re-ran the CLOUDFETCH-016 equivalent path:

[verify] raised=1 CloseStatement=1 DeleteSession=1

Out of scope (follow-ups)

  • Connector-side equivalent. The Python connector (databricks-sql-python) has the same shape of bug on the Thrift path — ThriftDatabricksClient.execute_command constructs the ResultSet inline, and a failing ThriftCloudFetchQueue.__init__ (which eagerly downloads chunk 0) propagates without calling close_command. That fix belongs in the connector repo and will be a separate PR.
  • Eager chunk-0 download in StreamingCloudFetchProvider::get_schema. The deeper design question is whether get_schema should download chunk 0 to extract the schema, or whether the schema can be derived from the SEA manifest alone. Lazy schema extraction would eliminate this entire failure mode (no download until next_batch). That's a larger change worth its own PR.

This pull request and its description were written by Isaac.

Downstream fixes / reviewer note

After ``call_execute_api`` succeeds the SEA statement is open on the
server. Two paths into a leak that would otherwise persist until
SQL Gateway's ``CommandInactivityTimeout`` (~1h):

1. ``wait_for_completion`` returns ``Error::timeout`` while the
   server still considers the statement live in PENDING / RUNNING.
   Terminal FAILED / CANCELED is the server's own cleanup signal and
   doesn't need a CloseStatement, but the polling-timeout path does.
2. ``create_reader`` fails. The most likely sub-failure is
   ``ResultPostProcessor::new`` -> ``StreamingCloudFetchProvider::
   get_schema`` eagerly downloading chunk 0 to extract the result
   schema; a non-retriable 4xx / persistent 5xx on that GET propagates
   up. The probe in driver-test PR #410's CLOUDFETCH-016 caught this:
   on a perpetual ``cloudfetch_500``, the kernel exhausted retries and
   never issued ``DELETE /api/2.0/sql/statements/{id}``.

Capture the ``statement_id`` from the initial response (before
``wait_for_completion``) and fire a best-effort close on both
failure paths via ``close_statement``. Since the trait method
already swallows its own errors, the wrapper here is just a logging
seam that attributes which phase failed so traces stay diagnosable.

New wiremock reproducer in ``tests/v0_execute_wiremock.rs``
(``execute_cloudfetch_chunk0_failure_closes_statement``): mounts a
SUCCEEDED execute response with external_links pointing at the mock
server, then 404s the chunk-0 GET. Asserts the mock observes a
DELETE on the statement. Test fails on baseline (5 chunk-0 GET
retries then no DELETE); passes with the fix in this commit.

End-to-end verified against the driver-test proxy suite's
CLOUDFETCH-016-equivalent path:

  raised=1 CloseStatement=1 DeleteSession=1

vs. baseline:

  raised=1 CloseStatement=0 DeleteSession=1

Closes the kernel half of the orphaned-op bug surfaced by
databricks/databricks-driver-test#410.

Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
Apply cargo fmt to arrow_parser.rs and fix the TlsConfig doc examples
in config.rs / session.rs, which used struct-literal syntax with
..default() against a #[non_exhaustive] struct — illegal from the
doc tests' external-crate context. Construct via TlsConfig::default()
and assign public fields instead.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
The rust-e2e workflow mapped DATABRICKS_HTTP_PATH from
secrets.TEST_PECO_WAREHOUSE_HTTP_PATH, but the azure-prod environment
has no secret by that name — its secrets are DATABRICKS_HOST,
DATABRICKS_HTTP_PATH, DATABRICKS_TOKEN. The unknown name resolved to
empty, so require_host_and_path() panicked with 'DATABRICKS_HTTP_PATH
must be set' and the whole v0 E2E suite failed. Point it at the
secret that actually exists.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
The check-tls-rustls job was the only cargo job in pr-checks.yml
missing the ./.github/actions/setup-jfrog step that its sibling
clippy/test jobs run. On the protected runners, direct crates.io
access is blocked (go/hardened-gha); without setup-jfrog the cargo
fetch has no db-cargo-remote config or OIDC token, so dependency
download fails with 'SSL connect error (unexpected eof while
reading)' fetching arrow-array — the recurring fast-fail seen on
every run. Add setup-jfrog and align the checkout / toolchain pins
with the other jobs.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
@vikrantpuppala
vikrantpuppala merged commit 5f5a908 into main Jun 1, 2026
12 of 13 checks passed

This branch had an error being deployed

1 failed deployment
azure-prod — 98db2153 Deployed Jun 1, 2026 by vikrantpuppala via v0 E2E surface #11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants