[SPARK-56019][SQL] Close JDBC connection on task kill to unblock native socket reads#55268
Closed
sadikovi wants to merge 2 commits intoapache:masterfrom
Closed
[SPARK-56019][SQL] Close JDBC connection on task kill to unblock native socket reads#55268sadikovi wants to merge 2 commits intoapache:masterfrom
sadikovi wants to merge 2 commits intoapache:masterfrom
Conversation
cloud-fan
reviewed
Apr 9, 2026
| } | ||
| conn.close() | ||
| if (!conn.isClosed) { | ||
| conn.close() |
Contributor
There was a problem hiding this comment.
closing a already closed connection is not noop?
Author
There was a problem hiding this comment.
Yes, you are right, it is based on the API doc. I just was not sure if I should wrap.
I removed, it also minimised the diff.
HyukjinKwon
approved these changes
Apr 9, 2026
cloud-fan
approved these changes
Apr 9, 2026
Contributor
|
the killed CI jobs are unrelated, thanks, merging to master! |
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.
What changes were proposed in this pull request?
Native JDBC socket reads (e.g.
socketRead0inResultSet.next()andPreparedStatement.executeBatch()) do not respond toThread.interrupt(). When the task reaper kills a task blocked in either call, the thread never unblocks and lingers indefinitely. This registers aTaskInterruptListeneron both the read path (JDBCRDD.compute, just beforeexecuteQuery()) and the write path (JdbcUtils.savePartition, just after the connection is opened). On kill, the listener closes theConnection, which tears down the underlying TCP socket and causes the blocked native call to throw aSQLException. The existingfinallyblocks in both paths are updated to tolerate a connection that was already closed by the listener.Why are the changes needed?
Tasks blocked in native JDBC reads silently ignore
Thread.interrupt(), so the task reaper cannot terminate them. The threads pile up, exhaust the executor's thread pool, and eventually hang the executor. Closing the connection from the interrupt-listener side is the correct fix: JDBC 4.0 §9.6 requires all methods on a closedConnectionto throwSQLException, and major drivers (PostgreSQL, MySQL, H2) implement this reliably. Closes SPARK-56019.Does this PR introduce any user-facing change?
No.
How was this patch tested?
Added
JdbcTaskInterruptSuitecovering both paths. Tests use mock JDBC objects andCountDownLatchto simulate threads blocked afterexecuteQuery()(inResultSet.next()) and inexecuteBatch(). Each test fires the interrupt listener while the mock is blocking, assertsconn.close()is called, and confirms the task thread unblocks and propagates theSQLException. A separate test verifies that arollback()failure on an already-closed connection does not mask the original exception.Was this patch authored or co-authored using generative AI tooling?
No.