Skip to content

Commit b9acb3e

Browse files
committed
SPARK-1572 Don't kill Executor if PythonRDD fails while computing parent
Previously, the behavior was that if the parent RDD threw any exception other than IOException or FileNotFoundException (which is quite possible for Hadoop input sources), the entire Executor would crash, because the default thread a uncaught exception handler calls System.exit(). This patch avoids two related issues: 1. Always catch exceptions in this reader thread. 2. Don't mask readerException when Python throws an EOFError after worker.shutdownOutput() is called.
1 parent 26d35f3 commit b9acb3e

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

core/src/main/scala/org/apache/spark/api/python/PythonRDD.scala

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.util.{List => JList, ArrayList => JArrayList, Map => JMap, Collectio
2424

2525
import scala.collection.JavaConversions._
2626
import scala.reflect.ClassTag
27+
import scala.util.Try
2728

2829
import net.razorvine.pickle.{Pickler, Unpickler}
2930

@@ -89,16 +90,17 @@ private[spark] class PythonRDD[T: ClassTag](
8990
dataOut.flush()
9091
worker.shutdownOutput()
9192
} catch {
92-
case e: java.io.FileNotFoundException =>
93-
readerException = e
94-
// Kill the Python worker process:
95-
worker.shutdownOutput()
9693
case e: IOException =>
9794
// This can happen for legitimate reasons if the Python code stops returning data
98-
// before we are done passing elements through, e.g., for take(). Just log a message
99-
// to say it happened.
100-
logInfo("stdin writer to Python finished early")
101-
logDebug("stdin writer to Python finished early", e)
95+
// before we are done passing elements through, e.g., for take(). Just log a message to
96+
// say it happened (as it could also be hiding a real IOException from a data source).
97+
logInfo("stdin writer to Python finished early (may not be an error)", e)
98+
99+
case e: Exception =>
100+
// We must avoid throwing exceptions here, because the thread uncaught exception handler
101+
// will kill the whole executor (see Executor).
102+
readerException = e
103+
Try(worker.shutdownOutput()) // kill Python worker process
102104
}
103105
}
104106
}.start()
@@ -152,7 +154,7 @@ private[spark] class PythonRDD[T: ClassTag](
152154
val exLength = stream.readInt()
153155
val obj = new Array[Byte](exLength)
154156
stream.readFully(obj)
155-
throw new PythonException(new String(obj))
157+
throw new PythonException(new String(obj), readerException)
156158
case SpecialLengths.END_OF_DATA_SECTION =>
157159
// We've finished the data section of the output, but we can still
158160
// read some accumulator updates:
@@ -167,10 +169,13 @@ private[spark] class PythonRDD[T: ClassTag](
167169
Array.empty[Byte]
168170
}
169171
} catch {
170-
case eof: EOFException => {
172+
case e: Exception if readerException != null =>
173+
logError("Python worker exited unexpectedly (crashed)", e)
174+
logError("Python crash may have been caused by prior exception:", readerException)
175+
throw readerException
176+
177+
case eof: EOFException =>
171178
throw new SparkException("Python worker exited unexpectedly (crashed)", eof)
172-
}
173-
case e: Throwable => throw e
174179
}
175180
}
176181

@@ -185,7 +190,7 @@ private[spark] class PythonRDD[T: ClassTag](
185190
}
186191

187192
/** Thrown for exceptions in user Python code. */
188-
private class PythonException(msg: String) extends Exception(msg)
193+
private class PythonException(msg: String, cause: Exception) extends RuntimeException(msg, cause)
189194

190195
/**
191196
* Form an RDD[(Array[Byte], Array[Byte])] from key-value pairs returned from Python.

0 commit comments

Comments
 (0)