Skip to content

Commit 7e2deb7

Browse files
Marcelo VanzinJoshRosen
authored andcommitted
[SPARK-4606] Send EOF to child JVM when there's no more data to read.
Author: Marcelo Vanzin <vanzin@cloudera.com> Closes apache#3460 from vanzin/SPARK-4606 and squashes the following commits: 031207d [Marcelo Vanzin] [SPARK-4606] Send EOF to child JVM when there's no more data to read.
1 parent 3f5f4cc commit 7e2deb7

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

core/src/main/scala/org/apache/spark/deploy/SparkSubmitDriverBootstrapper.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ private[spark] object SparkSubmitDriverBootstrapper {
151151
val isWindows = Utils.isWindows
152152
val isSubprocess = sys.env.contains("IS_SUBPROCESS")
153153
if (!isWindows) {
154-
val stdinThread = new RedirectThread(System.in, process.getOutputStream, "redirect stdin")
154+
val stdinThread = new RedirectThread(System.in, process.getOutputStream, "redirect stdin",
155+
propagateEof = true)
155156
stdinThread.start()
156157
// Spark submit (JVM) may run as a subprocess, and so this JVM should terminate on
157158
// broken pipe, signaling that the parent process has exited. This is the case if the

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,19 +1847,29 @@ private[spark] object Utils extends Logging {
18471847
/**
18481848
* A utility class to redirect the child process's stdout or stderr.
18491849
*/
1850-
private[spark] class RedirectThread(in: InputStream, out: OutputStream, name: String)
1850+
private[spark] class RedirectThread(
1851+
in: InputStream,
1852+
out: OutputStream,
1853+
name: String,
1854+
propagateEof: Boolean = false)
18511855
extends Thread(name) {
18521856

18531857
setDaemon(true)
18541858
override def run() {
18551859
scala.util.control.Exception.ignoring(classOf[IOException]) {
18561860
// FIXME: We copy the stream on the level of bytes to avoid encoding problems.
1857-
val buf = new Array[Byte](1024)
1858-
var len = in.read(buf)
1859-
while (len != -1) {
1860-
out.write(buf, 0, len)
1861-
out.flush()
1862-
len = in.read(buf)
1861+
try {
1862+
val buf = new Array[Byte](1024)
1863+
var len = in.read(buf)
1864+
while (len != -1) {
1865+
out.write(buf, 0, len)
1866+
out.flush()
1867+
len = in.read(buf)
1868+
}
1869+
} finally {
1870+
if (propagateEof) {
1871+
out.close()
1872+
}
18631873
}
18641874
}
18651875
}

0 commit comments

Comments
 (0)