Skip to content

fix(statement): skip redundant CloseStatement for CLOSED async results; cover inline sync path (#168) - #222

Merged
vikrantpuppala merged 2 commits into
mainfrom
fix/168-inline-redundant-close
Aug 10, 2026
Merged

vikrantpuppala merged 2 commits into
mainfrom
fix/168-inline-redundant-close

Conversation

@vikrantpuppala

@vikrantpuppala vikrantpuppala commented Aug 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #168 and PR #183. The original issue's metadata-path bug (redundant CloseStatement DELETE on a statement whose ExecuteStatement response already reported state=CLOSED) was fixed in #183. Investigating the two extensions raised later in the thread, I found:

  1. Inline sync-query path (CLOUDFETCH-013): already correct. Statement::execute computes already_closed = result.status.state == StatementState::Closed (since feat(statement): add directResults execute_direct (additive); CLOSED as terminal success #140) and pre-seeds the shared closed flag, so the drain auto-close, explicit close(), and Drop all short-circuit — no redundant DELETE. There was just no dedicated regression test on the wire.
  2. Async path (Statement::submit → ExecutedAsyncStatement): genuinely broken — same bug pattern, now fixed. The submit response uses wait_timeout=0s and can't return CLOSED, so closed correctly starts false; but reaching CLOSED via a later await_result() / fetch_result() poll never flipped it, so close() / Drop (and invalidate_prior on the next execute) fired a redundant DELETE on an already-closed statement.

Changes

Fix (src/statement/executed_async.rs): in materialise_result — the common sink for both await_result and fetch_result — pre-set closed = true when the fetched result's terminal status is CLOSED, mirroring the sync path's already_closed short-circuit. SUCCEEDED is unchanged (server still owns the statement → one CloseStatement still fires).

Tests:

  • tests/v0_async_execute_wiremock.rs
    • await_result_closed_status_skips_close_statement — verified tripwire: fails with one observed DELETE before the fix, passes after.
    • await_result_succeeded_status_still_closes_statement — guards against over-correcting into a leak.
  • tests/v0_execute_wiremock.rs (regression coverage for the already-correct sync path)
    • execute_closed_inline_skips_close_statement — verified tripwire (fails if already_closed is forced to false).
    • execute_succeeded_inline_still_closes_statement.

Sibling-path audit (issue #168 pattern)

Path CLOSED-skip Status
Metadata (StatementCloseGuard) ✅ Fixed in #183, tested
Sync execute() ✅ Already correct (already_closed); now tested here
Sync execute_direct() ✅ Already correct (shares the same already_closed line)
Async submit + await_result/fetch_result ✅ Fixed here
C ABI ✅ Delegates to the above handles; inherits the fix

On the async-close race (axis #1 in the thread)

The thread also raised that the close is a detached runtime.spawn, so the DELETE for a prior statement can race the next ExecuteStatement on a re-execute loop. I deliberately did not change this: it is not a correctness bug (the DELETE targets the prior, already-terminal statement; the next POST creates a fresh independent one — close_statement is scoped to a specific id and can't affect the new statement or session), it only occurs for non-CLOSED statements, and the fire-and-forget close is a deliberate, measured perf optimization (#85: removed Cancel → Close from the critical path, ~10-14% wall-clock win on large-result re-execute loops). The reported proxy-replay non-determinism was already handled downstream with a per-test connection. With this PR the common CLOSED case spawns no close at all, so there is nothing to race there.

Test plan

  • cargo test — 737 lib tests + all integration suites, 0 failures
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo +stable fmt --all
  • Both *_closed_* tests verified as tripwires (fail before the respective fix, pass after)

Closes #168
Closes #188

This pull request and its description were written by Isaac.

…OSED (#168)

Issue #168 (extended by conformance cell CLOUDFETCH-013) reported that a
regular inline query (`SELECT 1` delivered via direct results with
`status.state == CLOSED`) might still emit a redundant
`DELETE /api/2.0/sql/statements/<id>` (CloseStatement) on
`ExecutedStatement::close()` / drop, the same way the metadata path did
before PR #183.

The regular-query path is in fact already correct: `Statement::execute`
computes `already_closed = result.status.state == StatementState::Closed`
(since PR #140) and pre-seeds the shared `closed` flag, so neither the
drain auto-close, the explicit `close()`, nor `Drop` fires the redundant
DELETE. But there was no dedicated regression test asserting the wire
behavior on this path — only implicit coverage.

Add two wiremock regression tests mirroring the metadata suite:

- `execute_closed_inline_skips_close_statement` — a CLOSED inline
  `SELECT 1`, drained and explicitly closed, must produce ZERO DELETEs on
  the statement endpoint (expect(0) scoped mock + polled received_requests).
- `execute_succeeded_inline_still_closes_statement` — a SUCCEEDED inline
  result must STILL fire exactly one CloseStatement, guarding against the
  skip over-correcting into a server-statement leak.

The CLOSED-skip test is a verified tripwire: temporarily forcing
`already_closed = false` makes it fail with one observed DELETE, and it
passes on the real code.

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

…OSED (#168)

The async path (`Statement::submit` → `ExecutedAsyncStatement`) had the
same redundant-CloseStatement bug that #183 fixed for metadata and that
the sync `execute()` path already avoids: when the statement auto-closes
server-side and a poll observes `status.state == CLOSED`, the handle
never flipped its shared `closed` flag, so `close()` / `Drop` (and
`invalidate_prior` on the parent `Statement`'s next execute) fired a
redundant `DELETE /api/2.0/sql/statements/<id>` on an already-closed
statement (server responds 404 STATEMENT_NOT_FOUND).

The submit response itself uses `wait_timeout=0s` and can't return CLOSED,
so `closed` correctly starts `false`; the gap was that reaching CLOSED via
a later `await_result()` / `fetch_result()` poll never updated it.

Fix in `materialise_result` — the common sink for both `await_result` and
`fetch_result` — pre-set `closed = true` when the fetched result's
terminal status is `CLOSED`, mirroring the sync path's `already_closed`
short-circuit. `SUCCEEDED` is unchanged: the server still owns the
statement, so exactly one CloseStatement still fires.

Tests (tests/v0_async_execute_wiremock.rs):
- `await_result_closed_status_skips_close_statement` — CLOSED async result,
  drained + explicitly closed, must fire ZERO DELETEs. Verified tripwire:
  fails with one observed DELETE before the fix.
- `await_result_succeeded_status_still_closes_statement` — SUCCEEDED async
  result must STILL fire exactly one CloseStatement (no over-correction).

Co-authored-by: Isaac
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
@vikrantpuppala vikrantpuppala changed the title test(statement): lock in inline-query CloseStatement-skip on state=CLOSED (#168) fix(statement): skip redundant CloseStatement for CLOSED async results; cover inline sync path (#168) Aug 10, 2026

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

@vikrantpuppala
vikrantpuppala added this pull request to the merge queue Aug 10, 2026
Merged via the queue into main with commit 309e4c5 Aug 10, 2026
18 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — 305dc8ad Deployed Aug 10, 2026 by vikrantpuppala via review #95
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant