Skip to content

fix(bigquery-jdbc): harden Statement close and cancel concurrency - #15331

Open
keshavdandeva wants to merge 1 commit into
jdbc/fix-largeResultDataset-validationfrom
jdbc/large-results-2-statement-concurrency
Open

keshavdandeva wants to merge 1 commit into
jdbc/fix-largeResultDataset-validationfrom
jdbc/large-results-2-statement-concurrency

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

b/584840569

Summary of Changes

  1. Concurrency & lifecycle hardening (BigQueryStatement, BigQueryPreparedStatement, BigQueryBaseResultSet):
    • Separate cancel() from close() cleanly: cancel() cancels active jobs and closes currentResultSet without marking the statement closed or removing it from Connection.openStatements.
    • Snapshot this.connection (requireOpenConnection()) and guard close()/cancel() races across executeJob, handleQueryResult, processJsonResultSet, and processArrowResultSet so concurrent close throws a clean BigQueryJdbcException("Statement was closed.") instead of NullPointerException — without swallowing OutOfMemoryError or masking root causes.
    • Move cancelJobBestEffort(job) outside synchronized (cancelLock) in executeJob so cancelLock is never held across network RPCs.
    • Close prior currentResultSet cleanly on re-execution (prepareForExecution()), including BigQueryPreparedStatement.executeBatch(), and consolidate closeCurrentResultSet(boolean honorCloseOnCompletion).
    • Make parentJobId volatile, clear parentJobId alongside currentJobIdIndex = -1 under cancelLock in cancel() / closeStatementResources(), and snapshot parentJobId in hasMoreResults() / isSingularResultSet() so hasMoreResults() returns false after cancel().
    • Call checkNotClosedOrCanceled() before isSingularResultSet() in executeQueryImpl.
    • Add isSyntaxError(BigQueryException) helper and simplify isTerminalJobError(BigQueryException) to code == 400 || (code == 0 && ex.getError() != null) so non-terminal queryWithTimeout failures (e.g. 503) attempt best-effort cancel of the client JobId while terminal 400 errors skip unnecessary jobs.cancel RPCs.
  2. Tests (BigQueryStatementTest):
    • Focused tests for concurrent cancel()/close() during dataset check, getJob, getQueryResults, processJsonResultSet, processArrowResultSet, OutOfMemoryError propagation during close, script hasMoreResults() after cancel()/close(), and non-terminal vs. terminal queryWithTimeout cancellation.

Decisions Taken & Open Review Points

  • Statement.getConnection() after close() left unchanged in this stack: Existing BigQueryConnectionTest (line 1868) explicitly asserts assertNull(openStmt.getConnection()) after conn.abort(...). Fixing getConnection() to throw SQLException after close() (and updating BigQueryConnectionTest) is tracked separately in b/592249725.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces thread-safety and lifecycle improvements to the BigQuery JDBC driver, specifically targeting BigQueryStatement, BigQueryPreparedStatement, and BigQueryBaseResultSet. It transitions key state fields to volatile, introduces synchronized blocks on cancelLock to safely manage concurrent statement closing and cancellation, and refactors result set cleanup to properly support closeOnCompletion without stack overflows. The feedback highlights two critical concurrency issues: a race condition in closeCurrentResultSetAllowingCompletion() where this.currentResultSet is accessed without synchronization, and a non-atomic read-modify-write on this.currentJobIdIndex in getNextJob() that could lose concurrent cancellation signals.

@keshavdandeva
keshavdandeva force-pushed the jdbc/large-results-2-statement-concurrency branch from a7318c1 to 3096795 Compare December 1, 2026 20:09

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant