viirya opened a new pull request, #60540: URL: https://github.com/apache/spark/pull/60540
### What changes were proposed in this pull request? This is the follow-up promised in the review of SPARK-61720 (#60507). `BasePythonRunner.IdleTimeoutHandler` now runs the read from the worker itself instead of only inspecting the value a read returned: - `read(op: => Int)` replaces `throwIfKilled(n)`. It handles a socket read timeout (the retry loop that the pipelined stream had in `doRead`), and once the handler has killed the worker it reports both the end of the stream and an `IOException` of the read as the idle timeout, keeping the `IOException` as the cause. - `write(op: => Int)` does the same for an `IOException` of the writes that the selector based streams issue on the reading thread. - A failure with no kill is thrown unchanged, and so is anything that is not an `IOException`. The three streams that use the handler go through these two methods: `BasePythonRunner.ReaderInputStream`, the socket stream of `createPipelinedDataIn`, and `PythonPlannerRunner.WorkerInputStream`. ### Why are the changes needed? After the idle timeout kills a Python worker, the read from it does not always return the end of the stream. The connection of a killed worker can be reset, for example when the worker had unread input, and the read then throws an `IOException` (`Connection reset`). `throwIfKilled` was never reached in that case, so the failure went to `BasePythonRunner.handleException` (or to the `IOException` handler of `PythonPlannerRunner.runInPython`) and the task failed with ``` Python worker exited unexpectedly (crashed). Consider setting 'spark.sql.execution.pyspark.udf.faulthandler.enabled' ... ``` instead of ``` Python worker process terminated due to idle timeout (timeout: N seconds) ``` That hides the reason for the kill and points the user at a crash that did not happen. The reset is easy to hit: a UDF that hangs on its first batch while more input than the socket buffers hold is still being sent runs into it, since the killed worker has unread input. ### Does this PR introduce _any_ user-facing change? Yes. When a Python worker is killed because of the idle timeout and the read from it fails instead of returning the end of the stream, the task now fails with the idle timeout error (a `PythonWorkerException`, with the I/O failure as its cause) instead of "Python worker exited unexpectedly (crashed)". Read failures without an idle timeout kill are reported as before. ### How was this patch tested? New tests in `BasePythonRunnerSuite`, next to the existing `IdleTimeoutHandler` tests: - a read that fails after the kill, for a blocking socket with a read timeout (as in pipelined mode) and for a selector based read; the peer closes with `SO_LINGER` 0 so that the read really sees a connection reset; - a write that fails after the kill, and failures that are not I/O failures; - a read that fails with no kill: no timeout at all, `killOnIdleTimeout` disabled, a termination request that was not issued, and a worker that had already exited. The existing tests were updated from `throwIfKilled` to `read`. Also ran `pyspark.sql.tests.test_udf UDFTests.test_udf_kill_on_timeout`, `pyspark.sql.tests.test_udtf UDTFTests.test_udtf_kill_on_timeout` and `pyspark.sql.tests.pandas.test_pipelined_udf PipelinedUDFTests.test_udf_kill_on_idle_timeout`. Manually, a pandas UDF that sleeps past the idle timeout over a single partition of `spark.range(0, 50000000)` (non-pipelined) hits the reset: with this change the task fails with the idle timeout error, caused by `java.net.SocketException: Connection reset`. Not covered here: in pipelined mode, if the writer thread is still blocked sending input when the worker is killed, the writer fails as well and `handleException` rethrows `writer.exception` (a `Broken pipe`) ahead of the idle timeout error from the reader. Telling a writer that failed because of the kill from one that failed before it needs its own change. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (claude-fable-5-2) This pull request and its description were written by Isaac. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
