Repository navigation
fix(bigquery-jdbc): harden Statement close and cancel concurrency - #15331
keshavdandeva wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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.
80753a0 to
a7318c1
Compare
8e7c265 to
9db511a
Compare
a7318c1 to
3096795
Compare
9db511a to
40a2260
Compare
3096795 to
cc3dd3a
Compare
Standalone P2 bug fix (
b/593045100), decoupled from theLargeResultDataset/b/584840569stack (#15257→#15332) and tabled as a draft until after Seawall work lands (will targetmaincleanly once#15257merges).Summary of Latent Bugs Fixed (
b/593045100)Statement.cancel()detaches open statements fromConnection.openStatements: Separatecancel()fromclose()cleanly socancel()cancels active jobs and closescurrentResultSetwithout marking the statement closed or removing it fromConnection.openStatements.ResultSets and background reader workers: Close priorcurrentResultSetcleanly on re-execution (prepareForExecution()), includingBigQueryPreparedStatement.executeBatch(), and consolidatecloseCurrentResultSet(boolean honorCloseOnCompletion).Statement.close()during execution throws NPE and leaks workers: Snapshotthis.connection(requireOpenConnection()) and guardclose()/cancel()races acrossexecuteJob,handleQueryResult,processJsonResultSet, andprocessArrowResultSetso concurrent close throwsBigQueryJdbcException("Statement was closed.")instead ofNullPointerException— without swallowingOutOfMemoryErroror masking root causes.cancelJobBestEffort(job)outsidesynchronized (cancelLock)inexecuteJobsocancelLockis never held across network RPCs.isClosed,isCanceled,closeOnCompletion, andparentJobIdvolatile; clearparentJobIdalongsidecurrentJobIdIndex = -1undercancelLockincancel()/closeStatementResources(); synchronizecurrentJobIdIndexread-modify-write ingetNextJob(); snapshotparentJobIdinhasMoreResults()/isSingularResultSet()sohasMoreResults()returnsfalseaftercancel().checkNotClosedOrCanceled()beforeisSingularResultSet()inexecuteQueryImpl.isSyntaxError(BigQueryException)helper and simplifyisTerminalJobError(BigQueryException)tocode == 400 || (code == 0 && ex.getError() != null)so non-terminalqueryWithTimeout/getQueryResultsfailures (e.g.,503) attempt best-effort cancel while terminal400errors skip unnecessaryjobs.cancelRPCs.BigQueryStatementTest): Focused tests covering concurrentcancel()/close()across execution phases,OutOfMemoryErrorpropagation during close, scripthasMoreResults()aftercancel()/close(), and non-terminal vs. terminal cancellation.Follow-Up Issues
b/592249725— FixBigQueryStatement.getConnection()returningnullinstead of throwingSQLExceptionafterclose()(left unchanged here becauseBigQueryConnectionTest:1868currently assertsassertNull).