Skip to content

[SPARK-56586][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56772

Draft
HyukjinKwon wants to merge 1 commit into
apache:masterfrom
HyukjinKwon:ci-fix/agent7-foreachbatch
Draft

[SPARK-56586][CONNECT][SS] Abort wedged foreachBatch worker read on query stop#56772
HyukjinKwon wants to merge 1 commit into
apache:masterfrom
HyukjinKwon:ci-fix/agent7-foreachbatch

Conversation

@HyukjinKwon

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This PR makes a blocking read of the foreachBatch Python worker response in StreamingPythonRunner abortable, so that stopping a Spark Connect streaming query that uses foreachBatch reliably terminates even when the Python worker is wedged.

  • StreamingPythonRunner.readInterruptibly(dataIn) performs the per-batch readInt() 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 installed shouldAbortRead check trips. Stopping the worker closes its channel, which unblocks the in-flight read with an AsynchronousCloseException so 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.
  • The Connect foreachBatch cleaner cache (StreamingForeachBatchHelper.CleanerCache.registerCleanerForQuery) installs () => !query.isActive as that check when it registers a query's runner cleaner.
  • StreamingForeachBatchHelper now calls runner.readInterruptibly(dataIn) instead of dataIn.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 neither query.stop()'s Thread.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_TIMEOUT has no effect on channel reads). So stop() blocks until spark.sql.streaming.stopTimeout elapses; 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 to TERMINATED (so isActive becomes false) before it blocks waiting for the thread to die, so () => !query.isActive is 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 foreachBatch streaming 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.

…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 force-pushed the ci-fix/agent7-foreachbatch branch from d2e1089 to ac006c4 Compare June 25, 2026 14:27
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