Repository navigation
fix: reject statements on a closed connection before sending requests - #551
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 13, 2026 09:56
vuanhphung
enabled auto-merge
November 13, 2026 19:12
github-merge-queue
Bot
removed this pull request from the merge queue due to no response for status checks
Nov 13, 2026
vuanhphung
enabled auto-merge
November 13, 2026 21:47
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.
Calling
PrepareorPrepareContexton a closeddriver.Connreturned 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 withkernel_session_new_statement: session and out must be non-null. The conn kept no closed state. The Thrift backend also keeps its session handle afterCloseSession, soSessionValidstays true and can't be used to detect a closed conn.Closenow sets anatomic.Boolon the conn before callingCloseSession, so the conn is marked closed even if that call fails.Prepare,PrepareContext,ExecContextandQueryContextcheck the flag first and returndriver.ErrBadConnwrapping aDBDriverError("databricks: driver error: connection is closed"), without sending any request.ExecContextandQueryContextare guarded too because astmtdelegates execution to them, and Node.js (executeStatement), JDBC and Python (statements and cursors close with their connection) also reject execution after close.ErrBadConnfollows Go convention (go-sql-driver/mysql returns it fromPrepare/Exec/Queryon a closed conn) and is safe to return here: the request hasn't been sent, sodatabase/sqlcan retry on a fresh conn.database/sqlusers see no change, sincesql.Conn/sql.DBreturnsql.ErrConnDonebefore reaching the driver.Begin/BeginTxalready fail with "not implemented" and are unchanged.Verification:
NewConnector, and real kernel backend against a fake SEA TLS server. Each asserts zeroOpenSession/CreateSessionandExecuteStatementcalls for the rejected calls, and that a later conn opens its own session. All fail onmainand pass here, with-race.make test,make lintandgo vet(default anddatabricks_kerneltags) pass; the full kernel-tagged suite passes except failures that also occur onmain.TestE2EClosedConnRejectsStatements,TestKernelE2EClosedConnRejectsStatements), and the rest of both live suites shows no new failures.TestStatementCreationOnClosedConnectionRejected, through its counting proxy) changes from SKIP (xfail) onmainto PASS here.PrepareContextgoes from 0.16 to 1.42 ns/op, still with 0 allocs. A fullQueryContextround trip against the fake server is unchanged (306.0 µs vs 305.6 µs, p=0.96).Cross-driver state
throwExceptionIfConnectionIsClosed→CONNECTION_CLOSED, DatabricksConnection.java:1090 @0cb8c24; the session is marked closed in afinallyeven if DeleteSession fails (DatabricksSession.java:268-273)InterfaceError("Cannot create cursor from closed connection"), client.py:744-749 @5a1a7ec; closed cursors reject execute (client.py:1266)failIfClosed→HiveDriverError, DBSQLSession.ts:421-423 @93a9112; guardsexecuteStatementtoodriver.ErrBadConn+DBDriverError(Go has no SQLState surface); follows JDBC/Node semanticsOut of scope, possible follow-ups: making
Closeidempotent and havingIsValidreturn false afterClose.Adversarial review: converged in round 1 at 5a83977 with no findings.
Fixes #521
This pull request and its description were written by Isaac.