Repository navigation
perf(statement): close a complete inline result from execute(), not from the first fetch - #472
Open
vuanhphung wants to merge 2 commits into
Open
vuanhphung wants to merge 2 commits into
vuanhphung wants to merge 2 commits into
Conversation
…rom the first fetch
A SUCCEEDED response that already holds the whole result (an inline
attachment, at most one chunk, nothing to page) needs nothing more from
the server statement, so execute() now spawns its CloseStatement. The
Python binding runs execute() with the GIL released; before, the close
was spawned by the fetch that reached end-of-stream, with the GIL held.
An explicit close() before the stream is drained waits for that close,
as it waited for its own before; after a drain it returns at once, as
before. cancel() on a closed statement returns without a request.
Statement URLs are built from a {base}/statements URL parsed once the
session route is set, and discard_body takes a body that has already
arrived without arming its timer.
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
vuanhphung
temporarily deployed
to
azure-prod
November 15, 2026 00:55 — with
GitHub Actions
Inactive
There was a problem hiding this comment.
Verdict: 2 Low · 1 Nit
Looks good. The early close for a complete inline result is gated correctly: SUCCEEDED, an inline attachment, at most one chunk, nothing left to fetch. The closed, close() and Drop paths fit together, and no correctness bugs turned up. There are three low-severity notes: the retry-after-failed-early-close branch has no test, the warning logged when a close fails is misleading on the retry path, and a second concurrent close() can return Ok before the server has finished closing.
Other findings
- ⚪ Nit — Two concurrent
ExecutedStatementHandle::close(&self)calls behave differently. The first takes theJoinHandleand waits for the early close. The second seesNonewithclosed=trueand returnsOkat once, before the CloseStatement has finished, and even if it then fails and the first caller re-sends it. Before this change both callers would have hit the wire. The existing comment accepts racing closes, but the second caller now gets a prematureOk. Consider documenting this, or sharing the result (for example aSharedfuture ortokio::sync::OnceCell) if callers depend onclose()returning only after the server-side close.
…e-failure log close_resends_a_failed_early_close answers the first DELETE with a non-retryable 400 and checks that close() sends it again, returns Ok, and that dropping the handle afterwards sends nothing more. The background close's failure log no longer says the statement is abandoned: an explicit close() may still send it again. primary_keys_rejects_invalid_current_namespace_and_closes_lookup now uses a statement id per case. wiremock reuses servers across tests, and a late fire-and-forget close of another test's `stmt-test` could land on this one's server and break its expect(1). Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
vuanhphung
temporarily deployed
to
azure-prod
November 15, 2026 01:09 — with
GitHub Actions
Inactive
vuanhphung
temporarily deployed
to
azure-prod
November 15, 2026 01:09 — with
GitHub Actions
Inactive
vuanhphung
had a problem deploying
to
azure-prod
November 15, 2026 01:09 — with
GitHub Actions
Error
vuanhphung
had a problem deploying
to
azure-prod
November 15, 2026 01:09 — with
GitHub Actions
Error
This was referenced Nov 26, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Real-Time warehouses answer an inline
SELECTwithstate=SUCCEEDEDand the whole result in the response (checked on three staging warehouses, both dispositions, with and withoutwait_timeout; neverCLOSED), so the kernel sends a CloseStatement after every query. That close is spawned by the fetch that reaches end-of-stream, which the Python binding runs with the GIL held: a tokio task allocation and a worker wake per query, inside the section every Python thread of the process takes turns on. Each close also formats and re-parses its URL and arms a 500 ms timer to drain a two-byte{}body.Fix
When the execute response holds the whole result (SUCCEEDED, an inline attachment, at most one chunk, no next chunk or links),
execute()spawns the close itself. The bindings runexecute()without the GIL, and the drain and drop paths seeclosedand skip. An explicitclose()before the stream is drained waits for that close, as it waited for its own before; after a drain it returns at once, as before.cancel()on a closed statement returns without a request. Statement URLs come from a{base}/statementsURL parsed once the session route is set, anddiscard_bodytakes a body that has already arrived without arming its timer.Stacked on #459 (it changes
discard_body, which #459 adds).Verification
cargo fmt,cargo clippy --all-targets -D warningsandcargo testpass. New:execute_closes_complete_inline_result_before_it_is_read,close_waits_for_the_early_close_only_before_the_stream_is_drained, and unit tests for the completeness check and the URL builder.primary_keys_rejects_invalid_current_namespace_and_closes_lookupnow waits for the lookup's close before verifying: that lookup reads to the end, so its close is fire-and-forget (as before), and the test had relied on it landing before DeleteSession.Python driver end to end,
SELECT 1back to back in one process, on the full factory stack (#453 #455 #456 #457 #458 #459 #461 #467 #468 #469) with and without this change. Mock: a TLS server replaying the live Real-Time response bytes and headers (execute 80 ms, close 60 ms), client on 6 cores, 3 interleaved runs, medians:Live, staging X-Large Real-Time warehouse, 8 client cores, one run each: an env-gated prototype of this change together with the request-path changes in #470 (same code paths) cut client CPU per query at 1 process × 800 threads from 0.433 to 0.405 ms (-6.5%); steady QPS 7,651 → 7,961 and query p99 249 → 220 ms (that load is round-trip bound). Live, Medium Real-Time warehouse (server-bound at about 860 QPS), 1 × 200 threads, 60 s, 2 interleaved rounds, the exact commits of this PR, #470 and #471 together: 0.398 → 0.386 ms CPU per query, same QPS, same server-side error counts.
This pull request and its description were written by Isaac.