Skip to content

fix: reject statements on a closed connection before sending requests - #551

Merged
vuanhphung merged 2 commits into
mainfrom
fix/issue-521-closed-conn-prepare
Nov 13, 2026
Merged

vuanhphung merged 2 commits into
mainfrom
fix/issue-521-closed-conn-prepare

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Calling Prepare or PrepareContext on a closed driver.Conn returned a statement. The failure only showed up at execute: on Thrift, the server rejected the stale session handle with "Invalid SessionHandle", and on the kernel, the C ABI failed with kernel_session_new_statement: session and out must be non-null. The conn kept no closed state. The Thrift backend also keeps its session handle after CloseSession, so SessionValid stays true and can't be used to detect a closed conn.

Close now sets an atomic.Bool on the conn before calling CloseSession, so the conn is marked closed even if that call fails. Prepare, PrepareContext, ExecContext and QueryContext check the flag first and return driver.ErrBadConn wrapping a DBDriverError ("databricks: driver error: connection is closed"), without sending any request. ExecContext and QueryContext are guarded too because a stmt delegates execution to them, and Node.js (executeStatement), JDBC and Python (statements and cursors close with their connection) also reject execution after close. ErrBadConn follows Go convention (go-sql-driver/mysql returns it from Prepare/Exec/Query on a closed conn) and is safe to return here: the request hasn't been sent, so database/sql can retry on a fresh conn. database/sql users see no change, since sql.Conn/sql.DB return sql.ErrConnDone before reaching the driver. Begin/BeginTx already fail with "not implemented" and are unchanged.

Verification:

  • Unit and hermetic tests: real Thrift backend with a counting client, fake Thrift HTTP server through NewConnector, and real kernel backend against a fake SEA TLS server. Each asserts zero OpenSession/CreateSession and ExecuteStatement calls for the rejected calls, and that a later conn opens its own session. All fail on main and pass here, with -race.
  • Repo checks: make test, make lint and go vet (default and databricks_kernel tags) pass; the full kernel-tagged suite passes except failures that also occur on main.
  • Live e2e: passes on both backends (TestE2EClosedConnRejectsStatements, TestKernelE2EClosedConnRejectsStatements), and the rest of both live suites shows no new failures.
  • Conformance test: the Thrift leg of the driver-test SESSION-021 test (TestStatementCreationOnClosedConnectionRejected, through its counting proxy) changes from SKIP (xfail) on main to PASS here.
  • Performance (benchstat, n=8, alternating fresh processes): PrepareContext goes from 0.16 to 1.42 ns/op, still with 0 allocs. A full QueryContext round trip against the fake server is unchanged (306.0 µs vs 305.6 µs, p=0.96).

Cross-driver state

Driver State Notes
JDBC Supported throwExceptionIfConnectionIsClosed → CONNECTION_CLOSED, DatabricksConnection.java:1090 @0cb8c24; the session is marked closed in a finally even if DeleteSession fails (DatabricksSession.java:268-273)
Python Supported InterfaceError("Cannot create cursor from closed connection"), client.py:744-749 @5a1a7ec; closed cursors reject execute (client.py:1266)
Node.js Supported failIfClosed → HiveDriverError, DBSQLSession.ts:421-423 @93a9112; guards executeStatement too
Kernel Not applicable client-side lifecycle; the C ABI only null-checks (src/c_abi/session.rs:1904 @db23442)
Go Not supported → Supported (this PR) driver.ErrBadConn + DBDriverError (Go has no SQLState surface); follows JDBC/Node semantics

Out of scope, possible follow-ups: making Close idempotent and having IsValid return false after Close.

Adversarial review: converged in round 1 at 5a83977 with no findings.

Fixes #521

This pull request and its description were written by Isaac.

Prepare and PrepareContext on a closed driver.Conn returned a statement,
and execution then failed server-side (Thrift "Invalid SessionHandle")
or in the kernel C ABI (null session). Close now records a closed flag,
even when CloseSession fails. Prepare, PrepareContext, ExecContext and
QueryContext check it first and return driver.ErrBadConn wrapping a
"connection is closed" driver error, without sending OpenSession or
ExecuteStatement. This matches the JDBC, Python and Node.js drivers.

Fixes #521

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Vu Anh Phung <[email protected]>
@vuanhphung
vuanhphung marked this pull request as ready for review November 13, 2026 09:56

@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.

@vuanhphung vuanhphung added the skip-coverage Skip the coverage fan-out for this PR (no tracking issue opened in databricks-driver-test) label Nov 13, 2026
@vuanhphung
vuanhphung enabled auto-merge November 13, 2026 19:12

@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.

@vuanhphung
vuanhphung added this pull request to the merge queue Nov 13, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 13, 2026
@vuanhphung
vuanhphung enabled auto-merge November 13, 2026 21:47
@vuanhphung
vuanhphung added this pull request to the merge queue Nov 13, 2026
Merged via the queue into main with commit 370edb9 Nov 13, 2026
19 checks passed
@vuanhphung
vuanhphung deleted the fix/issue-521-closed-conn-prepare branch November 13, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-coverage Skip the coverage fan-out for this PR (no tracking issue opened in databricks-driver-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[coverage] Conformance findings: SESSION-021

1 participant