spark-test commented on code in PR #60540:
URL: https://github.com/apache/spark/pull/60540#discussion_r4593647200


##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -1384,12 +1455,30 @@ private[spark] abstract class BasePythonRunner[IN, OUT](
         throw new 
TaskKilledException(context.getKillReason().getOrElse("unknown reason"))
 
       case e: Exception if writer.exception.isDefined =>
-        logError(log"Python worker exited unexpectedly (crashed) - " +
-          log"${MDC(TASK_NAME, taskIdentifier(context))}", e)
-        logError(log"This may have been caused by a prior exception - " +
-          log"${MDC(TASK_NAME, taskIdentifier(context))}",
-          writer.exception.get)
-        throw writer.exception.get
+        val writerException = writer.exception.get
+        e match {
+          // The flag is read only after the exception was seen, which makes 
this consistent
+          // with a writer that fails concurrently; see `Writer.setException`.
+          case killed: PythonWorkerException if writer.failedWritingToWorker =>

Review Comment:
   **Non-blocking (P2):** This precedence only applies to failures that reach 
`handleException`. The checks at the start of `read()` in `PythonRunner` 
(L1950), `PythonUDFRunner` (L125) and `PythonArrowOutput` (L108) still do `if 
(writer.exception.isDefined) throw writer.exception.get` without looking at 
`failedWritingToWorker`. `hasNext` calls `read()` outside `catch 
handleException`, and a nested `read()` (after `METRICS_DATA`, 
`START_ARROW_STREAM` or the end of an Arrow stream) hands the writer's 
`IOException` to the `case _` branch below.
   
   Reading the code, this ordering gets through in pipelined mode with 
`killOnIdleTimeout`:
   
   1. The read times out, and the worker is killed.
   2. The retried read returns output that the worker flushed just before 
SIGTERM ended it. `destroy()` only sends the signal, and forked workers reset 
SIGTERM to `SIG_DFL`.
   3. The writer's blocked write fails on the reset and is recorded with 
`whileWritingToWorker = true` before the next `read()`.
   4. That `read()` throws the writer's bare `Broken pipe` or `Connection 
reset` instead of the idle-timeout error.
   
   The window is narrow, but it is the same misreport this PR fixes for 
pipelined mode. Unlike the window between the two case guards that the last 
round closed, no read runs here, so the handler's kill never reaches the 
decision.
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60540#discussion_r4581557640), 
[2](https://github.com/apache/spark/pull/60540#pullrequestreview-5914524590).
   
   **Recommended change:** Make the pre-read check of the writer's failure in 
the read() of PythonRunner, PythonUDFRunner and PythonArrowOutput apply 
handleException's precedence while it keeps throwing. When the writer has 
recorded a failure, the check reads writer.exception before 
Writer.failedWritingToWorker, as handleException does. If the failure is a 
failed write to the worker and the pipelined stream's IdleTimeoutHandler has 
killed the worker, the check throws that handler's idle-timeout 
PythonWorkerException with the writer's error added as suppressed; otherwise it 
throws the writer's failure, as on the base branch. Let the reader iterator 
reach the pipelined stream's handler, for example through a field of the Writer 
that createPipelinedDataIn sets, and let the handler build the error so that 
its message and termination diagnostics stay in one place. A small 
ReaderIterator helper next to handleException can hold the rule for the three 
read() implementations. Add Python runne
 r reader tests for a failed write to the worker after a kill, for the same 
failure without a kill, and for a failure producing the input after a kill.
   
   **Why this works:** handleException can choose correctly because it sees 
both failures: the reader's, which is the idle-timeout error only when the 
handler killed the worker, and the writer's flag. The pre-read check sees only 
the writer's failure, and it cannot read on to learn of the kill. A writer that 
has failed shuts down its output. While the connection is still open, the 
worker then takes the end of its input as the end of the data and can finish 
normally on partial input, and the check in the read() that follows the 
worker's METRICS_DATA is what keeps the task from completing. Asking the 
pipelined stream's handler whether it killed the worker gives the check the one 
fact it lacks while it still throws, and letting that handler build the error 
keeps its message in one place.
   
   **Scope:** Make the reader iterators' pre-read check of the pipelined 
writer's failure report the idle timeout after a kill, using the pipelined 
stream's handler, while it still throws for every writer failure, with reader 
tests for each outcome.
   
   **Compatibility:** Every recorded writer failure still fails the next read 
before it reads from the stream, as on the base branch. Only the error changes, 
and only for a failed write to the worker after the pipelined stream's handler 
killed the worker. Without a kill, and in non-pipelined mode, every error and 
log is reported exactly as on the base branch.
   
   **Risks:** The reader iterator now reaches the pipelined stream's 
IdleTimeoutHandler through state the two share, such as a field of the Writer, 
and both use it only on the task thread. A stream that does not register its 
handler keeps the base-branch report, the writer's error, and does not read on. 
Reverting the check restores the old order. The idle-timeout error that the 
check throws before a read has no cause, since no read failed; the writer's I/O 
error is attached to it as suppressed.
   
   **Constraints:** Read writer.exception before writer.failedWritingToWorker, 
the reverse of the order in which the writer sets them; otherwise a writer that 
fails between the two reads can still win. The pre-read check must still throw 
whenever the writer has recorded a failure, and choose only between the 
writer's error and the idle-timeout error. Reading on would let a worker that 
saw the end of its input after the writer shut down its output finish normally 
on partial input. A writer failure that is not a failed write to the worker, 
such as a failure producing the input, a ClosedChannelException or a failure in 
open(), still throws the writer's error, also after a kill. Use the handler 
only on the thread that reads from the worker, as its doc requires; the reader 
iterator runs on that thread.
   
   **Exclusions:**
   - The check at the start of handleInitResponse stays as it is: it runs once, 
before the first read, so no idle-timeout kill can have happened yet.
   - Non-pipelined mode and pipelined mode over Unix domain sockets keep their 
behavior: only the pipelined writer marks a failed write to the worker, and 
over Unix domain sockets the pipelined stream has no idle-timeout handler.
   
   **Success:** In pipelined mode with killOnIdleTimeout, when a read returns 
after the kill and the writer has recorded a failed write to the worker, the 
next read() throws the idle-timeout PythonWorkerException before reading from 
the stream, with the writer's error suppressed once, also through a nested 
read(). Without a kill, a recorded failed write to the worker is still thrown 
before the next read, exactly as on the base branch. A writer failure that is 
not a failed write to the worker is still thrown before the read, also after a 
kill.
   
   **Verification:**
   - **Regression:** A reader iterator whose pipelined writer recorded a failed 
write to the worker, and whose pipelined stream's handler has killed the 
worker, throws the idle-timeout error from its next read() without reading the 
stream, with the writer's error suppressed once also when a nested read() 
throws it; the test fails if the check throws the writer's error instead.
   - **Compatibility:** With the same recorded failed write but no kill, and 
with a writer that failed producing the input after a kill, the next read() 
throws the writer's error without reading the stream, as on the base branch.



##########
core/src/main/scala/org/apache/spark/api/python/PythonRunner.scala:
##########
@@ -702,16 +704,82 @@ private[spark] object BasePythonRunner extends Logging {
     }
 
     /**
-     * Throws a [[PythonWorkerException]] if `n`, the result of a read from 
the worker, is the
-     * end of the stream and the worker was killed because of the idle timeout.
+     * Reads from the channel of the worker into `buf`, like 
`worker.channel.read(buf)`. Once
+     * the worker has been killed because of the idle timeout, the end of the 
stream and a
+     * failure of the read both throw a [[PythonWorkerException]] for the idle 
timeout: the
+     * connection of a killed worker can be reset instead of closed, for 
example when the worker
+     * had unread input. A failure of the read with no kill is thrown as is.

Review Comment:
   **Nit (P3):** After the kill, only an `IOException` for which `causedByKill` 
holds becomes the idle-timeout error. A `ClosedChannelException` (including 
`ClosedByInterruptException` and `AsynchronousCloseException`) and any failure 
that is not an `IOException` are rethrown unchanged, as the new test at 
L1252-L1274 of `BasePythonRunnerSuite` checks. This doc, the `write` doc at 
L725-L727, and `readBlocking` through "behaves like `read`" still say that 
every failure after the kill is reported as the idle timeout, which is the 
broader attribution this PR dropped in review. Something like:
   
   ```suggestion
        * Reads from the channel of the worker into `buf`, like 
`worker.channel.read(buf)`. Once
        * the worker has been killed because of the idle timeout, the end of 
the stream and an
        * IOException of the read, other than a ClosedChannelException (see 
`causedByKill`), both
        * throw a [[PythonWorkerException]] for the idle timeout: the 
connection of a killed worker
        * can be reset instead of closed. Any other failure, or one with no 
kill, is thrown as is.
   ```
   
   The `write` doc needs the same exclusion.
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60540#discussion_r4581557646), 
[2](https://github.com/apache/spark/pull/60540#pullrequestreview-5914524590).
   
   **Verification:**
   - **Inspection:** The read and write docs, which readBlocking's doc refers 
to, describe the same exclusions as causedByKill, and no doc of the handler 
says that every failure after the kill becomes the idle-timeout error.



##########
core/src/test/scala/org/apache/spark/api/python/BasePythonRunnerSuite.scala:
##########
@@ -975,14 +969,15 @@ class BasePythonRunnerSuite extends SparkFunSuite with 
SharedSparkContext {
           nonBlockingWorkerStatus + "hasInputs = true"))
       assert(handle.destroyCalls.get() === 1)
 
-      // Only the end of the stream fails the read.
-      handler.throwIfKilled(0)
-      handler.throwIfKilled(16)
+      // A read that returns data, or no data yet, does not fail; the end of 
the stream does.
+      assert(handler.readBlocking(0) === 0)
+      assert(handler.readBlocking(16) === 16)
       val e = intercept[PythonWorkerException] {
-        handler.throwIfKilled(-1)
+        handler.readBlocking(-1)

Review Comment:
   **Nit (P3):** The selector streams, `ReaderInputStream` and the planner's 
`WorkerInputStream`, now get their end-of-stream check from `read(buf)` (L720 
of `PythonRunner.scala`). This test and the other selector cases at L999, L1020 
and L1038 assert it through `readBlocking`, which only the pipelined stream 
calls. On the base branch they called `throwIfKilled`, the method the selector 
streams used, so the check was pinned. Every new `read(buf)` call here ends in 
a reset or a closed channel (L1154, L1219, L1269, and the `ReaderInputStream` 
test), so removing `throwIfKilledAtEndOfStream(n)` from `read(buf)` would keep 
this suite green. The regression would then show up only in the PySpark kill 
tests, and only when the killed worker closes its connection instead of 
resetting it. The selector cases need to drive `read(buf)` itself to the end of 
the stream; `readBlocking` can stay for the blocking-socket cases.
   
   This extends existing discussions 
[1](https://github.com/apache/spark/pull/60540#discussion_r4581557665), 
[2](https://github.com/apache/spark/pull/60540#pullrequestreview-5914524590).
   
   **Verification:**
   - **Regression:** After the kill, a selector handler whose peer closes the 
connection normally throws the idle-timeout error with no cause from read(buf), 
and the assertion fails if read(buf) stops checking the end of the stream.
   - **Behavior:** Without a kill, read(buf) on a connection the peer closed 
normally returns -1.



-- 
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]

Reply via email to