Github user srowen commented on the pull request: https://github.com/apache/spark/pull/11628#issuecomment-196741708 Here's an alternate take in light of comments above, to illustrate what I'm thinking of: ``` ... val proc = pb.start() val env = SparkEnv.get val childThreadException = new AtomicReference[Throwable]() // Start a thread to print the process's stderr to ours new Thread(s"stderr reader for $command") { override def run(): Unit = { val err = proc.getErrorStream try { for (line <- Source.fromInputStream(err).getLines) { // scalastyle:off println System.err.println(line) // scalastyle:on println } } catch { case t: Throwable => childThreadException.set(t) } finally { err.close() } } }.start() // Start a thread to feed the process input from our parent's iterator new Thread(s"stdin writer for $command") { override def run(): Unit = { TaskContext.setTaskContext(context) val out = new PrintWriter(proc.getOutputStream) try { // scalastyle:off println // input the pipe context firstly if (printPipeContext != null) { printPipeContext(out.println) } for (elem <- firstParent[T].iterator(split, context)) { if (printRDDElement != null) { printRDDElement(elem, out.println) } else { out.println(elem) } } // scalastyle:on println } catch { case t: Throwable => childThreadException.set(t) } finally { out.close() } } }.start() // Return an iterator that read lines from the process's stdout val lines = Source.fromInputStream(proc.getInputStream).getLines() new Iterator[String] { def next(): String = { if (!hasNext()) { throw new NoSuchElementException() } lines.next() } def hasNext(): Boolean = { propagateChildException() if (lines.hasNext) { true } else { val exitStatus = proc.waitFor() cleanup() if (exitStatus != 0) { throw new IllegalStateException(s"Subprocess exited with status $exitStatus") } false } } private def cleanup(): Unit = { // cleanup task working directory if used if (workInTaskDirectory) { scala.util.control.Exception.ignoring(classOf[IOException]) { Utils.deleteRecursively(new File(taskDirectory)) } logDebug(s"Removed task working directory $taskDirectory") } } private def propagateChildException(): Unit = { val t = childThreadException.get() if (t != null) { proc.destroy() cleanup() throw t } } } ... ```
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org