Skip to content

SEA metadata queries emit a redundant CloseStatement DELETE on a statement already returned state=CLOSED #168

Closed

Description

@eric-wang-1990

Summary

The kernel's SEA metadata path (Metadata::list_catalogs / list_schemas / list_tables / list_columns) issues a redundant DELETE /api/2.0/sql/statements/<id> (CloseStatement) on a statement that the ExecuteStatement response already reported status.state == CLOSED. The kernel's regular-query path correctly recognizes CLOSED and skips the close; the metadata path does not.

Evidence (proxy-captured wire traffic, driver-test suite)

A metadata call sequence on one session:

CreateSession
ExecuteStatement   → response status.state = CLOSED   (SHOW … , inline/direct result)
ExecuteStatement   → response status.state = CLOSED
CloseStatement     → DELETE /api/2.0/sql/statements/<id>   ← REDUNDANT (statement already CLOSED)
ExecuteStatement   → response status.state = CLOSED
DeleteSession

Contrast — a regular SELECT 1 on the same kernel:

CreateSession
ExecuteStatement   → response status.state = CLOSED
DeleteSession                                            ← NO CloseStatement (correctly skipped)

Both responses carry status.state = CLOSED. Per the SEA API contract, CLOSED means "execution successful, and statement closed; result no longer available for fetch" — so once the response is CLOSED, the statement is already closed server-side and must not be closed again.

Expected

The metadata result path should honor status.state == CLOSED from the ExecuteStatement response and skip the follow-up CloseStatement, matching the regular-query path (and matching the databricks-jdbc SEA metadata path, which runs metadata fully-sync via the x-databricks-sea-can-run-fully-sync header and emits no separate close).

Impact

Redundant server round-trip per metadata call. It also surfaced as a non-deterministic replay failure in the databricks-driver-test Python proxy suite: on a connection shared across tests, the close is deferred to the next metadata call and its DELETE bleeds into the next test's recording context. (Worked around in that repo with a per-test connection; this issue tracks the underlying kernel behavior.)

Found via the databricks-driver-test kernel proxy fan-out.

Activity

  1. eric-wang-1990 commented on Jul 16, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Additional evidence: request-shape difference vs C# ADBC (which does NOT hit this)

    Comparing the metadata ExecuteStatement the kernel sends against the C# ADBC SEA client's, on the same warehouse, both disposition=INLINE_OR_EXTERNAL_LINKS, format=ARROW_STREAM:

    C# ADBC SEA (metadata request):

    • body includes on_wait_timeout: "CONTINUE"
    • header x-databricks-sea-can-run-fully-sync: true
    • response status.state = SUCCEEDED → statement is still open → C# then sends CloseStatement (correct, non-redundant)

    Kernel (metadata request):

    • body has no on_wait_timeout
    • no x-databricks-sea-can-run-fully-sync header
    • response status.state = CLOSED → statement already closed server-side → kernel still sends CloseStatement (redundant)

    So the kernel's metadata request omits both the on_wait_timeout=CONTINUE field and the x-databricks-sea-can-run-fully-sync header that C#/JDBC send. Regardless of the exact server-side reason the kernel's response comes back CLOSED, the defect is clear: when the ExecuteStatement response reports status.state == CLOSED, the metadata path should skip the follow-up CloseStatement — exactly as the kernel's regular-query path already does. (The C# path legitimately closes because its statements come back SUCCEEDED, not CLOSED.)

    Two possible fixes, either resolves the redundant DELETE:

    1. Skip CloseStatement when the response already reported state == CLOSED (minimal; mirrors the regular-query path).
    2. Align the metadata request with C#/JDBC (on_wait_timeout=CONTINUE + x-databricks-sea-can-run-fully-sync) so behavior is consistent across SEA clients.
  2. eric-wang-1990 commented on Jul 16, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Correction / clarification after live curl testing

    I tried to reproduce the CLOSED-vs-SUCCEEDED difference with raw curl against POST /api/2.0/sql/statements and want to correct my previous comment, which over-attributed the state difference to the header/on_wait_timeout alone:

    • On the dispositions the public REST endpoint accepts (INLINE / JSON_ARRAY, EXTERNAL_LINKS / ARROW_STREAM), every combination of {x-databricks-sea-can-run-fully-sync header, on_wait_timeout=CONTINUE, default/30s wait} returned SUCCEEDED — I could not produce CLOSED with the header or on_wait_timeout alone. (wait_timeout=0s → PENDING, as expected.)
    • The public endpoint rejects disposition=INLINE_OR_EXTERNAL_LINKS (INVALID_PARAMETER_VALUE: … is not a supported disposition), which is exactly the disposition both drivers use — so I could not exercise the server path that yields CLOSED via plain curl.

    So the precise trigger for CLOSED couldn't be isolated externally. What the recordings do establish (same warehouse, identical disposition=INLINE_OR_EXTERNAL_LINKS + format=ARROW_STREAM):

    client on_wait_timeout x-databricks-sea-can-run-fully-sync resp status.state redundant CloseStatement?
    C# ADBC SEA CONTINUE true SUCCEEDED no (statement open → close is correct)
    kernel absent absent CLOSED yes (already closed)

    The core defect is unchanged and does not depend on why the state differs: when the ExecuteStatement response reports status.state == CLOSED, the kernel's metadata path should not send a follow-up CloseStatement — the kernel's own regular-query path already skips it. Aligning the metadata request with C#/JDBC (on_wait_timeout=CONTINUE + the fully-sync header) is a plausible secondary fix, but I have NOT proven those flags are what flips the state — treat that as a hypothesis, not a confirmed cause.

  3. eric-wang-1990 commented on Jul 16, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Live curl testing overturns the "kernel differs from C#" framing — please re-scope this issue

    Correcting my earlier comments. Using a real driver User-Agent (the public REST endpoint rejects INLINE_OR_EXTERNAL_LINKS without one — thanks to the reviewer who pointed this out), I re-ran the metadata ExecuteStatement live against the warehouse and isolated every axis:

    With disposition=INLINE_OR_EXTERNAL_LINKS, format=ARROW_STREAM, the response is status.state = CLOSED in ALL cases:

    • kernel UA vs C# ADBC UA → both CLOSED
    • with vs without x-databricks-sea-can-run-fully-sync: true → both CLOSED
    • with vs without on_wait_timeout=CONTINUE → both CLOSED
    • SHOW SCHEMAS, SELECT CURRENT_CATALOG() (C#'s exact metadata statement), SELECT 1 → all CLOSED

    So the header / on_wait_timeout / user-agent do NOT drive the state, and the kernel is not sending a materially different request. My earlier hypothesis was wrong.

    The SUCCEEDED seen in the C# ADBC recording is stale: that tape was captured 2026-07-08. The server's behavior for this disposition has since changed to return CLOSED for inline/small results. A fresh C# re-record today would also get CLOSED and would also emit the same follow-up CloseStatement.

    Re-scoped issue (still valid, but not kernel-vs-C#): the server now reports status.state == CLOSED on the ExecuteStatement response for hybrid-disposition metadata queries, meaning the statement is already closed server-side. The kernel's metadata result path still issues a redundant DELETE /statements/<id> on it — whereas the kernel's own regular-query path already skips the close when the result arrives CLOSED. So the ask is a small optimization: honor state == CLOSED in the metadata path too and skip the redundant CloseStatement, matching the regular-query path. It is NOT a behavioral divergence from other SEA clients (they'd all do the same today).

    (This surfaced only as a replay-ordering artifact in databricks-driver-test, already fixed there by using a per-test connection so the close records in-context; no functional impact live, since closing an already-closed statement returns 200.)

  4. eric-wang-1990 commented on Jul 16, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Confirmed via C# ADBC driver source: this IS a real divergence — C#/JDBC skip the close, the kernel doesn't

    I inspected the C# ADBC SEA driver (StatementExecutionStatement.cs) directly. It explicitly handles state == CLOSED and skips the redundant close:

    // Set when the server returns state=CLOSED (direct-result delivery): the result is already
    // in-hand and the statement is closed server-side, so Dispose must NOT re-close it.
    private bool _statementClosedByServer;
    ...
    // CLOSED = execution succeeded and the statement is already closed server-side; ... remember
    // it's closed so Dispose skips the redundant CloseStatement. Matches databricks-jdbc, which
    // treats CLOSED == SUCCEEDED.
    if (state == "CLOSED")
    {
        ...
        _statementClosedByServer = true;
    }

    The CloseStatement DELETE on dispose is then gated on _statementClosedByServer — when the server already reported CLOSED, C# does not send it. (They even reset the flag per-execution so a later genuinely-open SUCCEEDED statement still gets its legitimate close.)

    So my previous "all SEA clients would do the same today" comment was wrong. The correct picture:

    client sees state == CLOSED sends redundant CloseStatement?
    C# ADBC yes no — _statementClosedByServer guard skips it (matches JDBC: CLOSED == SUCCEEDED)
    databricks-jdbc yes no
    kernel yes yes ← the bug

    (The SUCCEEDED in the older adbc-csharp recording is just stale tape from before the server started returning CLOSED; the C# driver code checks the live state, so it handles today's CLOSED correctly.)

    Fix: the kernel's metadata (and any) result path should set an equivalent "already closed server-side" flag when the ExecuteStatement response reports state == CLOSED, and skip the follow-up DELETE /statements/<id> — matching C# ADBC and databricks-jdbc.

  5. eric-wang-1990 commented on Jul 16, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Cross-driver comparison: C# ADBC and JDBC both close SYNCHRONOUSLY and skip the redundant close — the kernel does neither

    Confirmed the close behavior in the two reference SEA drivers vs the kernel:

    C# ADBC (StatementExecutionStatement.cs):

    if (!alreadyClosed)
    {
        // Close statement synchronously during dispose
        _client.CloseStatementAsync(_currentStatementId, CancellationToken.None).GetAwaiter().GetResult();
    }

    → synchronous (blocks on the DELETE) AND guarded by !alreadyClosed.

    JDBC (DatabricksStatement.close()):

    // Skip server-side close if operation was already closed:
    // - directResultsReceived: server closed it (inline results)
    // - serverOperationClosed: client proactively closed it (results consumed or RS closed)
    if (!directResultsReceived && !serverOperationClosed) {
        ...getDatabricksClient().closeStatement(statementId);
    }

    → synchronous (closeStatement runs apiClient.execute(req) inline) AND skips when the server already closed it.

    Kernel (src/statement/executed.rs::spawn_close_if_open, src/result_stream.rs::StatementCloseGuard::drop):

    runtime.spawn(async move {
        if let Err(e) = transport.close_statement(&stmt_id).await { warn!(...); }
    });

    → detached fire-and-forget task (async) AND fires unconditionally (no state==CLOSED guard).

    So the kernel diverges from BOTH reference drivers on TWO axes:

    1. Async vs sync close. C#/JDBC block on the close during dispose, so it always lands in order relative to subsequent requests. The kernel runtime.spawns it detached, so the DELETE /statements/<id> races the connector's next request — non-deterministic wire ordering. Proven behaviorally: the identical proxy recording replays fail/pass/pass across 3 back-to-back runs (the DELETE sometimes lands before, sometimes after, the next ExecuteStatement).

    2. No skip-when-already-closed. As above — C# uses !alreadyClosed, JDBC uses !directResultsReceived && !serverOperationClosed. The kernel has no equivalent, so it redundantly closes statements the ExecuteStatement response already reported state=CLOSED.

    Fixing (2) alone (skip the close when the response is CLOSED, matching JDBC's directResultsReceived) removes the redundant close for all metadata queries and eliminates the observed non-determinism there, since no close would be spawned. Fixing (1) — making the close synchronous like C#/JDBC, or at least ordering it before the next request — addresses the broader latent race for the cases where a close IS needed (e.g. cloud-fetch/large results).

  6. added
    engineer-bot
    on Jul 21, 2026
  7. oss-engineer-bot commented on Jul 21, 2026

    @oss-engineer-bot

    🤖 engineer-bot opened a fix PR: #183

    Review before merge.

  8. eric-wang-1990 commented on Jul 24, 2026

    @eric-wang-1990
    CollaboratorAuthor

    Extends this issue: the redundant CloseStatement also fires on the inline direct-results path (regular query), not just metadata.

    This issue's summary notes the regular-query path correctly recognizes state=CLOSED and skips the close, scoping the bug to metadata. But conformance test CLOUDFETCH-013 (databricks-driver-test) shows a plain SELECT 1 — delivered inline via direct results with status.state == CLOSED — still gets a redundant DELETE /api/2.0/sql/statements/{id} on ExecutedStatement::close():

    CreateSession
    ExecuteStatement   → response status.state = CLOSED   (SELECT 1, inline direct results)
    … drain rows …
    close()            → DELETE /api/2.0/sql/statements/{id}   ← redundant (already CLOSED)
    

    The test asserts CloseStatement == 0 on the inline path and currently fails (committed as an xfail tripwire). So the redundant-close applies to at least: (1) metadata queries, and (2) inline direct-results regular queries. The fix should suppress the close whenever the ExecuteStatement response already reported state=CLOSED, regardless of path.

    (No action needed from the kernel side beyond what's already planned — just flagging the broader trigger so the fix covers the inline path too. The conformance suite tracks this cell against THIS issue.)

    [posted via the driver-test conformance workflow]

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

    engineer-bot

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions