Skip to content

Fire-and-forget (async spawned) CloseStatement on drain/Drop races the session's next request — non-deterministic wire ordering #170

Closed as not planned

Description

@eric-wang-1990

Summary

The kernel closes a statement via a detached, fire-and-forget async task on the auto-close-on-drain and Drop paths (runtime.spawn(async move { transport.close_statement(...).await })), rather than awaiting the close at a well-defined point. Because the spawned DELETE /api/2.0/sql/statements/<id> runs on the runtime whenever it's scheduled, it races the connector's next request on the same session, producing non-deterministic wire ordering.

This is distinct from #168 (which is that the close is redundant when the statement is already state=CLOSED). This issue is about the close that does fire being async rather than synchronous.

Source (HEAD 56b9a6e)

Fire-and-forget spawn sites:

  • src/statement/executed.rs:165 — spawn_close_if_open() (auto-close-on-drain + Drop)
  • src/result_stream.rs:505 — StatementCloseGuard::drop() (metadata streams)
  • src/statement/executed_async.rs:811 (close), :631 (cancel)

Contrast — the explicit close path already awaits synchronously:

  • pyo3/src/statement.rs:266 — run_kernel!(py, runtime, stmt.close())
  • pyo3/src/statement.rs:392 — run_kernel!(py, runtime, inner.exec.close())

So the kernel already has a synchronous close; the drain/Drop paths just don't use it.

Evidence of the race

In the databricks-driver-test kernel proxy suite, the identical committed recording replays fail / pass / pass across three back-to-back runs (strict-ordering replay): the spawned CloseStatement DELETE sometimes lands before, sometimes after, the next ExecuteStatement. That non-determinism is only possible if the close is not ordered w.r.t. subsequent requests.

Comparison with the other Databricks SEA drivers (both synchronous)

  • C# ADBC (StatementExecutionStatement.cs): CloseStatementAsync(...).GetAwaiter().GetResult() — comment: "Close statement synchronously during dispose."
  • JDBC (DatabricksStatement.close()): apiClient.execute(req, ...) inline (blocking).

Both close synchronously at result-set/statement close, so the DELETE never races a later request.

Why it matters

  • Non-deterministic wire ordering breaks ordered replay/proxy tooling and makes traffic hard to reason about / debug.
  • Latent beyond metadata: any multi-statement flow on one session (e.g. cloud-fetch/large results followed by another query) can hit the same race.

Suggested fix

Close synchronously on the drain / explicit-close path (await it, as the stmt.close() path already does and as C#/JDBC do). Reserve the fire-and-forget runtime.spawn only for the true Drop-without-explicit-close GC fallback (where Rust can't .await), not as the primary mechanism for a normally-consumed statement.

Note: fixing #168 (skip the close entirely when the ExecuteStatement response is state=CLOSED) removes the race for the metadata case outright, since no close would be spawned there. This issue covers the remaining cases where a close genuinely must fire.

Found via the databricks-driver-test kernel proxy fan-out. Related: #168.

Activity

  1. vikrantpuppala commented on Aug 10, 2026

    @vikrantpuppala
    Collaborator

    Recommendation: working as intended — the async close is a deliberate optimization, not a correctness bug

    I prototyped the synchronous-close fix (#224, now closed) and, on review, it's the wrong trade. Laying out why, because the distinction matters:

    The race has no correctness impact

    The auto-close fires DELETE /api/2.0/sql/statements/<id> against a specific, already-terminal statement_id. The caller's next request is a POST /statements that creates an independent statement with a different id. So the two operations are independent server-side:

    • The DELETE cannot affect the next statement (different id).
    • The DELETE cannot affect the session.
    • The prior statement is already terminal, so nothing is lost regardless of ordering.
    • Session close (delete_session) does not coordinate with or wait on pending statement closes — and the server reclaims every statement on session delete anyway.

    Whether the DELETE lands before or after the next POST is therefore invisible to any client program. Nothing in production behaves differently.

    What the async close actually is

    It's a deliberate, measured optimization — PR #85 moved Cancel + Close off the critical path of re-execute loops (~10–14% wall-clock win on large-result QRC loops; Execute → CloudFetch → Cancel → Close became Execute → CloudFetch on the hot path, with the close in flight while the consumer is already issuing the next query). C#/JDBC close synchronously, but the kernel intentionally chose async for latency.

    Why the synchronous fix is a bad trade

    Making the drain close synchronous (as #224 did) re-introduces serially the CloseStatement RTT that #85 moved off the critical path — a real +1 RTT at end-of-stream for every SUCCEEDED / large-CloudFetch statement the caller drains. (Quick state=CLOSED inline queries are unaffected, since #168 already suppresses their close entirely.) That's paying production latency to make an external replay harness's recording deterministic.

    The only real symptom, and where it belongs

    The non-determinism only affects strict-ordering proxy replay in databricks-driver-test — a test-infrastructure concern. That repo already has the appropriate fix (per-test / per-connection recording, as noted here and in #168). The kernel shouldn't slow down production traffic to accommodate a replay harness's ordering assumption about two independent requests.

    If ordering determinism ever becomes worth it

    A non-blocking approach is possible: keep the close spawned, but have the next request on the session await any still-pending close before issuing — deterministic ordering with ~0 hot-path latency, preserving #85. Happy to implement that if the replay determinism turns out to cause ongoing pain that the per-connection workaround doesn't cover. Absent that, it's added complexity for a non-problem.

    Recommendation: close as working-as-intended. The complementary redundant-close bug (#168) — where a close fires that genuinely shouldn't — is the real defect and is fixed. This issue is about the ordering of a close that is harmless regardless of when it lands.

  2. github-actions commented on Sep 9, 2026

    @github-actions

    This issue has been marked as Stale because it has been open for 30 days with no activity. If you would like the issue to remain open, please remove the stale label or comment on the issue.

  3. github-actions commented on Oct 10, 2026

    @github-actions

    This issue has been marked as Stale because it has been open for 30 days with no activity. If you would like the issue to remain open, please remove the stale label or comment on the issue.

  4. github-actions commented on Oct 18, 2026

    @github-actions

    This issue was closed because it has been inactive for 7 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions