[DO-NOT-MERGE][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56799
Closed
HyukjinKwon wants to merge 1 commit into
Closed
[DO-NOT-MERGE][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56799HyukjinKwon wants to merge 1 commit into
HyukjinKwon wants to merge 1 commit into
Conversation
…uery stop SparkConnectSessionHolderSuite 'python foreachBatch process: process terminates after query is stopped' flakily fails (and previously hung for ~150 minutes) because query.stop() cannot terminate a wedged micro-batch thread. Root cause: the micro-batch execution thread runs the foreachBatch function, which blocks in StreamingPythonRunner reading the Python worker response via Channels.newInputStream(channel).readInt(). When the worker is wedged (e.g. deadlocked on a re-entrant Spark Connect call), that read is broken by neither query.stop()'s Thread.interrupt() (the channel is not closed by interrupting the reader, and the interrupt flag is never even observed on the stream thread) nor a socket read timeout (SO_TIMEOUT has no effect on channel reads). So stop() blocks until spark.sql.streaming.stopTimeout elapses (default: infinite -> hangs forever). SPARK-56586's bound+retry only turns the hang into a failure; the retries deadlock identically. Fix: StreamingPythonRunner.readInterruptibly guards the blocking read with a watchdog thread. The Connect foreachBatch cleaner cache installs () => !query.isActive when it registers a query's cleaner, so the watchdog knows when the query is being stopped (state is set to TERMINATED before stop() blocks). Once that abort condition has held for a short grace period -- long enough for a responsive worker to finish the in-flight batch and let the read complete cleanly, so a normal stop is undisturbed -- the watchdog stops the worker; closing the worker channel unblocks a genuinely wedged read with an AsynchronousCloseException and the batch/query unwinds promptly. A legitimately slow batch (query still active) is never killed. Co-authored-by: Isaac
Member
Author
|
Closing as a duplicate of #56772, which carries the same foreachBatch worker-read abort fix and is already CI-validated. Let's consolidate discussion there. |
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?
This PR makes
StreamingPythonRunner's blocking read of the Python worker response interruptible when a Spark ConnectforeachBatchquery is being stopped.StreamingPythonRunner.readInterruptiblyguards the blockingChannels.newInputStream(channel).readInt()with a watchdog thread. The ConnectforeachBatchcleaner cache installs an abort condition() => !query.isActivewhen it registers a query's cleaner. Once that abort condition has held for a short grace period (long enough for a responsive worker to finish the in-flight batch so a normal stop is undisturbed), the watchdog closes the worker channel, which unblocks a genuinely wedged read with anAsynchronousCloseExceptionso the batch/query unwinds promptly. A legitimately slow batch (query still active) is never killed.Why are the changes needed?
SparkConnectSessionHolderSuite—python foreachBatch process: process terminates after query is stoppedflakily fails (and previously hung for ~150 minutes) becausequery.stop()cannot terminate a wedged micro-batch thread.The micro-batch execution thread runs the
foreachBatchfunction, which blocks inStreamingPythonRunnerreading the Python worker response. When the worker is wedged (e.g. deadlocked on a re-entrant Spark Connect call), that read is broken by neitherquery.stop()'sThread.interrupt()(interrupting the reader does not close the channel, and the interrupt flag is never observed on the stream thread) nor a socket read timeout (SO_TIMEOUThas no effect on channel reads). Sostop()blocks untilspark.sql.streaming.stopTimeoutelapses (default: infinite → hangs forever). The previous bound+retry only turned the hang into a failure; the retries deadlock identically.This is one of the two remaining unmerged fixes keeping the apache/spark master matrix builds red — it fails the
connectmodule on the scheduled Maven JDK 25 and JDK 21 (ARM) lanes.Does this PR introduce any user-facing change?
No. The change only affects how a wedged Python worker read is aborted on query stop; a normal stop is undisturbed.
How was this patch tested?
connectmodule tests on a fork, including the previously-flakySparkConnectSessionHolderSuiteforeachBatch termination test (also repeated x12 in a focused workflow to deflake).Build / Maven (Scala 2.13, JDK 25)—connectmodule,StreamingForeachBatchHelperSuiteforeachBatch termination test times out (spark.sql.streaming.stopTimeout): https://github.com/apache/spark/actions/runs/28179702547connectmodule green on the fork — the formerly-hangingpython foreachBatch process: process terminates after query is stoppednow passes in ~5.5s (connect module:Tests: succeeded 1612, failed 0): https://github.com/HyukjinKwon/spark/actions/runs/28204993346/job/83555910566Was this patch authored or co-authored using generative AI tooling?
Yes.
This pull request and its description were written by Isaac.