Fix/spark 58751 listener shutdown - #58217
Open
nahtonaj wants to merge 2 commits into
Open
Conversation
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 pull request handles state server shutdown while the listener is still waiting for the Python worker to connect.
The patch makes the following changes:
TransformWithStateInPySparkPythonRunner.stop()so that it interrupts the state server listener thread before closing the server socket channel. This matches the existing executor-side cleanup ordering.ServerSocketChannel.accept()before the Python worker connects.CLOSED.ClosedChannelException, including itsAsynchronousCloseExceptionsubclass, as an expected shutdown path without incorrectly setting the interrupt status.ServerSocketChanneltests for both interruption before enteringaccept()and interruption while the listener is blocked insideaccept(). The tests verify bounded listener termination, no escaped exception, channel closure, theCLOSEDhandle state, and no protocol output.No public APIs or configuration options are added or changed.
Why are the changes needed?
When a TransformWithState in PySpark runner is stopped before the Python worker connects to its JVM state server, the listener can still be blocked in
ServerSocketChannel.accept().Previously, the driver-side cleanup closed the server socket channel before interrupting the listener thread. Closing the channel wakes the listener with an expected
AsynchronousCloseException. Because the pre-connect path did not handle this exception, it escapedTransformWithStateInPySparkStateServer.run()and was wrapped as:This reports an orderly shutdown as an unexpected daemon-thread failure. The same listener implementation is used by the executor-side lifecycle, where interruption can produce
ClosedByInterruptException.The patch makes both expected shutdown paths explicit. It interrupts before closing, handles the relevant Java NIO exception hierarchy, restores the interrupt flag only for interrupt-driven shutdown, and transitions the processor handle to its terminal
CLOSEDstate.Does this PR introduce any user-facing change?
Yes.
Previously, stopping a TransformWithState in PySpark query before its Python worker connected could report the expected state server listener shutdown as an unexpected daemon-thread crash.
After this change, the state server listener terminates cleanly during this shutdown window. Expected interruption or channel-closure exceptions no longer surface as a spurious
SparkException.This is a bug fix relative to released Spark versions as well as the behavior on the current
masterbranch. It does not change the TransformWithState API or successful query execution behavior.How was this patch tested?
Added tests to
TransformWithStateInPySparkStateServerSuitecovering:InterruptedException,InterruptedIOException, andClosedByInterruptExceptionwhile waiting for the Python worker, including preservation of the listener thread's interrupt status.AsynchronousCloseExceptionandClosedChannelExceptionwhile waiting for the Python worker, including verification that the listener thread is not marked interrupted.ServerSocketChannelwith the listener thread already interrupted before enteringaccept().ServerSocketChannelwith the listener confirmed to be blocked insideaccept()before applying the production-equivalent interrupt-then-close shutdown sequence.StatefulProcessorHandleState.CLOSED, and no protocol output for the real-channel shutdown paths.The following local static checks passed:
dev/lint-scalacompleted both the Scalastyle and Scalafmt checks successfully.The focused suite can be run with:
build/sbt -Phive -Phive-thriftserver \ "sql/core/testOnly org.apache.spark.sql.execution.python.streaming.TransformWithStateInPySparkStateServerSuite"The focused SBT suite could not be executed in the local development environment because Maven Central and the SBT repository hosts failed DNS resolution while retrieving the required SBT launcher and dependencies. The generated partial launcher file was removed from the worktree. The suite is expected to run in GitHub Actions, where the build dependencies are available.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: databricks-gpt-5-6-sol on KiroCrew 0.2.0-customapi.5