[SPARK-56586][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56772
Draft
HyukjinKwon wants to merge 1 commit into
Draft
[SPARK-56586][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56772HyukjinKwon 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
d2e1089 to
ac006c4
Compare
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 a blocking read of the foreachBatch Python worker response in
StreamingPythonRunnerabortable, so that stopping a Spark Connect streaming query that usesforeachBatchreliably terminates even when the Python worker is wedged.StreamingPythonRunner.readInterruptibly(dataIn)performs the per-batchreadInt()under a small watchdog thread. The watchdog stops the worker once the read should be abandoned — either the calling (micro-batch) thread is interrupted, or an installedshouldAbortReadcheck trips. Stopping the worker closes its channel, which unblocks the in-flight read with anAsynchronousCloseExceptionso the batch (and the query) can unwind promptly. When no abort is requested the read blocks normally, so a legitimately slow batch is never killed.StreamingForeachBatchHelper.CleanerCache.registerCleanerForQuery) installs() => !query.isActiveas that check when it registers a query's runner cleaner.StreamingForeachBatchHelpernow callsrunner.readInterruptibly(dataIn)instead ofdataIn.readInt().Why are the changes needed?
SparkConnectSessionHolderSuite"python foreachBatch process: process terminates after query is stopped" is flaky, and the underlying behavior is a real hang.The micro-batch execution thread runs the foreachBatch function, which blocks reading the Python worker response via
Channels.newInputStream(channel).readInt(). When the worker is wedged (e.g. it is mid re-entrant Spark Connect call), that read is broken by neitherquery.stop()'sThread.interrupt()(the channel is not closed by interrupting the reader thread, and the interrupt flag is not even observed on the stream thread) nor a socket read timeout (SO_TIMEOUThas no effect on channel reads). Sostop()blocks untilspark.sql.streaming.stopTimeoutelapses; with the default (infinite) timeout it hangs indefinitely. SPARK-56586 bounded the test with a stop timeout and retries, but that only turns the hang into a failure — the retries deadlock identically.query.stop()sets the query state toTERMINATED(soisActivebecomesfalse) before it blocks waiting for the thread to die, so() => !query.isActiveis a reliable signal that lets the watchdog break the wedged read promptly.Does this PR introduce any user-facing change?
No. Stopping a Spark Connect
foreachBatchstreaming query is now reliable where it could previously hang, but there is no API or behavior change for successful queries.How was this patch tested?
Repeated the previously-flaky test on a fork runner:
SparkConnectSessionHolderSuite"python foreachBatch process: process terminates after query is stopped" passed 12/12 times on the first attempt with no retries (it previously failed even after 3 retries). Root cause and the close→unblock mechanism were also confirmed with targeted diagnostics on the same path.Was this patch authored or co-authored using generative AI tooling?
Yes, generated by Isaac.