Skip to content

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

Draft
keshavdandeva wants to merge 2 commits into
mainfrom
jdbc/large-results-2-statement-concurrency
Draft

keshavdandeva wants to merge 2 commits into
mainfrom
jdbc/large-results-2-statement-concurrency

Conversation

@keshavdandeva

@keshavdandeva keshavdandeva commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Standalone P2 bug fix (b/593045100), decoupled from the LargeResultDataset / b/584840569 stack (#15257 → #15332) and tabled as a draft until after Seawall work lands (will target main cleanly once #15257 merges).


Summary of Latent Bugs Fixed (b/593045100)

  1. Statement.cancel() detaches open statements from Connection.openStatements: Separate cancel() from close() cleanly so cancel() cancels active jobs and closes currentResultSet without marking the statement closed or removing it from Connection.openStatements.
  2. Re-execution leaks open ResultSets and background reader workers: Close prior currentResultSet cleanly on re-execution (prepareForExecution()), including BigQueryPreparedStatement.executeBatch(), and consolidate closeCurrentResultSet(boolean honorCloseOnCompletion).
  3. Concurrent Statement.close() during execution throws NPE and leaks workers: Snapshot this.connection (requireOpenConnection()) and guard close()/cancel() races across executeJob, handleQueryResult, processJsonResultSet, and processArrowResultSet so concurrent close throws BigQueryJdbcException("Statement was closed.") instead of NullPointerException — without swallowing OutOfMemoryError or masking root causes.
  4. Concurrency races & script cursor state bugs:
    • Move cancelJobBestEffort(job) outside synchronized (cancelLock) in executeJob so cancelLock is never held across network RPCs.
    • Make isClosed, isCanceled, closeOnCompletion, and parentJobId volatile; clear parentJobId alongside currentJobIdIndex = -1 under cancelLock in cancel() / closeStatementResources(); synchronize currentJobIdIndex read-modify-write in getNextJob(); snapshot parentJobId in hasMoreResults() / isSingularResultSet() so hasMoreResults() returns false after cancel().
    • Call checkNotClosedOrCanceled() before isSingularResultSet() in executeQueryImpl.
  5. Orphaned BigQuery jobs & null-message syntax check NPE: Add isSyntaxError(BigQueryException) helper and simplify isTerminalJobError(BigQueryException) to code == 400 || (code == 0 && ex.getError() != null) so non-terminal queryWithTimeout/getQueryResults failures (e.g., 503) attempt best-effort cancel while terminal 400 errors skip unnecessary jobs.cancel RPCs.
  6. Tests (BigQueryStatementTest): Focused tests covering concurrent cancel()/close() across execution phases, OutOfMemoryError propagation during close, script hasMoreResults() after cancel()/close(), and non-terminal vs. terminal cancellation.

Follow-Up Issues

  • b/592249725 — Fix BigQueryStatement.getConnection() returning null instead of throwing SQLException after close() (left unchanged here because BigQueryConnectionTest:1868 currently asserts assertNull).

@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/fix-largeResultDataset-validation branch from 9db511a to 40a2260 Compare December 2, 2026 15:38
@keshavdandeva
keshavdandeva force-pushed the jdbc/large-results-2-statement-concurrency branch from 3096795 to cc3dd3a Compare December 2, 2026 15:38
@keshavdandeva
keshavdandeva marked this pull request as draft December 3, 2026 16:21
@keshavdandeva
keshavdandeva removed this pull request from stack #15336 December 3, 2026 17:17
@keshavdandeva
keshavdandeva changed the base branch from jdbc/fix-largeResultDataset-validation to main December 3, 2026 17:19

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