Skip to content

[DO-NOT-MERGE][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56799

Closed
HyukjinKwon wants to merge 1 commit into
apache:masterfrom
HyukjinKwon:ci-fix/agent5afa-pr-foreachbatch
Closed

[DO-NOT-MERGE][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56799
HyukjinKwon wants to merge 1 commit into
apache:masterfrom
HyukjinKwon:ci-fix/agent5afa-pr-foreachbatch

Conversation

@HyukjinKwon

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This PR makes StreamingPythonRunner's blocking read of the Python worker response interruptible when a Spark Connect foreachBatch query is being stopped.

StreamingPythonRunner.readInterruptibly guards the blocking Channels.newInputStream(channel).readInt() with a watchdog thread. The Connect foreachBatch cleaner cache installs an abort condition () => !query.isActive when 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 an AsynchronousCloseException so the batch/query unwinds promptly. A legitimately slow batch (query still active) is never killed.

Why are the changes needed?

SparkConnectSessionHolderSuitepython 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.

The micro-batch execution thread runs the foreachBatch function, which blocks in StreamingPythonRunner reading the Python worker response. 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() (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_TIMEOUT has no effect on channel reads). So stop() blocks until spark.sql.streaming.stopTimeout elapses (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 connect module 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?

connect module tests on a fork, including the previously-flaky SparkConnectSessionHolderSuite foreachBatch termination test (also repeated x12 in a focused workflow to deflake).

Was this patch authored or co-authored using generative AI tooling?

Yes.

This pull request and its description were written by Isaac.

…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
@HyukjinKwon HyukjinKwon changed the title [SPARK-56586][CONNECT][SS] Abort wedged foreachBatch worker read on query stop [DO-NOT-MERGE][CONNECT][SS] Abort wedged foreachBatch worker read on query stop Jun 26, 2026
@HyukjinKwon HyukjinKwon marked this pull request as draft June 26, 2026 03:25
@HyukjinKwon

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant