Skip to content

Commit

Permalink
[8406] ProcessBuilderImpl catches exceptions thrown on background thread
Browse files Browse the repository at this point in the history
[8406] Adds more caught exceptions in Simple ProcessBuilder#run
  • Loading branch information
joshlemer committed Sep 20, 2018
1 parent 35f71fa commit 8b30c14
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/library/scala/sys/process/ProcessBuilderImpl.scala
Expand Up @@ -68,7 +68,17 @@ private[process] trait ProcessBuilderImpl {
/** Represents a simple command without any redirection or combination. */
private[process] class Simple(p: JProcessBuilder) extends AbstractBuilder {
override def run(io: ProcessIO): Process = {
val process = p.start() // start the external process
val process = try {
p.start() // start the external process
} catch {
case _: IndexOutOfBoundsException
| _: IOException
| _: NullPointerException
| _: SecurityException
| _: UnsupportedOperationException
=> return FailedProcess
}

import io._

// spawn threads that process the input, output, and error streams using the functions defined in `io`
Expand Down
6 changes: 6 additions & 0 deletions src/library/scala/sys/process/ProcessImpl.scala
Expand Up @@ -255,4 +255,10 @@ private[process] trait ProcessImpl {
override def exitValue() = if (success.get) 0 else 1 // thread.join()
override def destroy() = thread.interrupt()
}

private[process] object FailedProcess extends Process {
override def isAlive() = false
override def exitValue() = 1
override def destroy(): Unit = ()
}
}
17 changes: 17 additions & 0 deletions test/junit/scala/sys/process/ProcessBuilderTest.scala
@@ -0,0 +1,17 @@
package scala.sys.process

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test

@RunWith(classOf[JUnit4])
class ProcessBuilderTest {

@Test
def t8406: Unit = {
import java.io.ByteArrayInputStream
import scala.util.Try
assert(Try(("does-not-exist" #< new ByteArrayInputStream("foo".getBytes("utf-8"))).lazyLines).isSuccess)
assert(Try(("does-not-exist" #< new ByteArrayInputStream("foo".getBytes("utf-8"))).lazyLines.toList).isFailure)
}
}

0 comments on commit 8b30c14

Please sign in to comment.