Repository navigation
fix(bigquery-jdbc): harden Statement close and cancel concurrency - #15331
keshavdandeva wants to merge 1 commit 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
a7318c1 to
3096795
Compare
b/584840569
Summary of Changes
BigQueryStatement,BigQueryPreparedStatement,BigQueryBaseResultSet):cancel()fromclose()cleanly:cancel()cancels active jobs and closescurrentResultSetwithout marking the statement closed or removing it fromConnection.openStatements.this.connection(requireOpenConnection()) and guardclose()/cancel()races acrossexecuteJob,handleQueryResult,processJsonResultSet, andprocessArrowResultSetso concurrent close throws a cleanBigQueryJdbcException("Statement was closed.")instead ofNullPointerException— without swallowingOutOfMemoryErroror masking root causes.cancelJobBestEffort(job)outsidesynchronized (cancelLock)inexecuteJobsocancelLockis never held across network RPCs.currentResultSetcleanly on re-execution (prepareForExecution()), includingBigQueryPreparedStatement.executeBatch(), and consolidatecloseCurrentResultSet(boolean honorCloseOnCompletion).parentJobIdvolatile, clearparentJobIdalongsidecurrentJobIdIndex = -1undercancelLockincancel()/closeStatementResources(), and snapshotparentJobIdinhasMoreResults()/isSingularResultSet()sohasMoreResults()returnsfalseaftercancel().checkNotClosedOrCanceled()beforeisSingularResultSet()inexecuteQueryImpl.isSyntaxError(BigQueryException)helper and simplifyisTerminalJobError(BigQueryException)tocode == 400 || (code == 0 && ex.getError() != null)so non-terminalqueryWithTimeoutfailures (e.g.503) attempt best-effort cancel of the clientJobIdwhile terminal400errors skip unnecessaryjobs.cancelRPCs.BigQueryStatementTest):cancel()/close()during dataset check,getJob,getQueryResults,processJsonResultSet,processArrowResultSet,OutOfMemoryErrorpropagation during close, scripthasMoreResults()aftercancel()/close(), and non-terminal vs. terminalqueryWithTimeoutcancellation.Decisions Taken & Open Review Points
Statement.getConnection()afterclose()left unchanged in this stack: ExistingBigQueryConnectionTest(line 1868) explicitly assertsassertNull(openStmt.getConnection())afterconn.abort(...). FixinggetConnection()to throwSQLExceptionafterclose()(and updatingBigQueryConnectionTest) is tracked separately inb/592249725.