From c576aedae92e4231588d883dba3d49170696c6fa Mon Sep 17 00:00:00 2001 From: Bell Le Date: Fri, 16 Aug 2024 16:54:29 -0700 Subject: [PATCH] Handle all failures in Future --- .../frontend/PluginFrontend.scala | 20 +++---- .../frontend/PosixPluginFrontend.scala | 5 ++ .../frontend/OsSpecificFrontendSpec.scala | 52 ++++++++++++++----- .../frontend/PosixPluginFrontendSpec.scala | 6 ++- .../frontend/WindowsPluginFrontendSpec.scala | 6 ++- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala b/bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala index 7415f06..ec7d6ca 100644 --- a/bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala +++ b/bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala @@ -5,8 +5,6 @@ import java.nio.file.{Files, Path} import protocbridge.{ProtocCodeGenerator, ExtraEnv} -import scala.util.Try - /** A PluginFrontend instance provides a platform-dependent way for protoc to * communicate with a JVM based ProtocCodeGenerator. * @@ -47,13 +45,7 @@ object PluginFrontend { gen: ProtocCodeGenerator, request: Array[Byte] ): Array[Byte] = { - Try { - gen.run(request) - }.recover { case throwable => - createCodeGeneratorResponseWithError( - throwable.toString + "\n" + getStackTrace(throwable) - ) - }.get + gen.run(request) } def createCodeGeneratorResponseWithError(error: String): Array[Byte] = { @@ -116,9 +108,17 @@ object PluginFrontend { gen: ProtocCodeGenerator, fsin: InputStream, env: ExtraEnv - ): Array[Byte] = { + ): Array[Byte] = try { val bytes = readInputStreamToByteArrayWithEnv(fsin, env) runWithBytes(gen, bytes) + } catch { + // This covers all Throwable including OutOfMemoryError, StackOverflowError, etc. + // We need to make a best effort to return a response to protoc, + // otherwise protoc can hang indefinitely. + case throwable: Throwable => + createCodeGeneratorResponseWithError( + throwable.toString + "\n" + getStackTrace(throwable) + ) } def createTempFile(extension: String, content: String): Path = { diff --git a/bridge/src/main/scala/protocbridge/frontend/PosixPluginFrontend.scala b/bridge/src/main/scala/protocbridge/frontend/PosixPluginFrontend.scala index 5f70120..ef57687 100644 --- a/bridge/src/main/scala/protocbridge/frontend/PosixPluginFrontend.scala +++ b/bridge/src/main/scala/protocbridge/frontend/PosixPluginFrontend.scala @@ -40,6 +40,11 @@ object PosixPluginFrontend extends PluginFrontend { val response = PluginFrontend.runWithInputStream(plugin, fsin, env) fsin.close() + // Note that the output pipe must be opened after the input pipe is consumed. + // Otherwise, there might be a deadlock that + // - The shell script is stuck writing to the input pipe (which has a full buffer), + // and doesn't open the write end of the output pipe. + // - This thread is stuck waiting for the write end of the output pipe to be opened. val fsout = Files.newOutputStream(outputPipe) fsout.write(response) fsout.close() diff --git a/bridge/src/test/scala/protocbridge/frontend/OsSpecificFrontendSpec.scala b/bridge/src/test/scala/protocbridge/frontend/OsSpecificFrontendSpec.scala index 5b158ac..1519857 100644 --- a/bridge/src/test/scala/protocbridge/frontend/OsSpecificFrontendSpec.scala +++ b/bridge/src/test/scala/protocbridge/frontend/OsSpecificFrontendSpec.scala @@ -10,20 +10,14 @@ import scala.util.Random class OsSpecificFrontendSpec extends AnyFlatSpec with Matchers { - protected def testPluginFrontend(frontend: PluginFrontend): Array[Byte] = { - val random = new Random() - val toSend = Array.fill(123)(random.nextInt(256).toByte) - val toReceive = Array.fill(456)(random.nextInt(256).toByte) - val env = new ExtraEnv(secondaryOutputDir = "tmp") - - val fakeGenerator = new ProtocCodeGenerator { - override def run(request: Array[Byte]): Array[Byte] = { - request mustBe (toSend ++ env.toByteArrayAsField) - toReceive - } - } + protected def testPluginFrontend( + frontend: PluginFrontend, + generator: ProtocCodeGenerator, + env: ExtraEnv, + request: Array[Byte] + ): Array[Byte] = { val (path, state) = frontend.prepare( - fakeGenerator, + generator, env ) val actualOutput = new ByteArrayOutputStream() @@ -32,7 +26,7 @@ class OsSpecificFrontendSpec extends AnyFlatSpec with Matchers { .run( new ProcessIO( writeInput => { - writeInput.write(toSend) + writeInput.write(request) writeInput.close() }, processOutput => { @@ -53,4 +47,34 @@ class OsSpecificFrontendSpec extends AnyFlatSpec with Matchers { frontend.cleanup(state) actualOutput.toByteArray } + + protected def testSuccess(frontend: PluginFrontend): Unit = { + val random = new Random() + val toSend = Array.fill(123)(random.nextInt(256).toByte) + val toReceive = Array.fill(456)(random.nextInt(256).toByte) + val env = new ExtraEnv(secondaryOutputDir = "tmp") + + val fakeGenerator = new ProtocCodeGenerator { + override def run(request: Array[Byte]): Array[Byte] = { + request mustBe (toSend ++ env.toByteArrayAsField) + toReceive + } + } + val response = testPluginFrontend(frontend, fakeGenerator, env, toSend) + response mustBe toReceive + } + + protected def testFailure(frontend: PluginFrontend): Unit = { + val random = new Random() + val toSend = Array.fill(123)(random.nextInt(256).toByte) + val env = new ExtraEnv(secondaryOutputDir = "tmp") + + val fakeGenerator = new ProtocCodeGenerator { + override def run(request: Array[Byte]): Array[Byte] = { + throw new OutOfMemoryError("test error") + } + } + val response = testPluginFrontend(frontend, fakeGenerator, env, toSend) + response.length must be > 0 + } } diff --git a/bridge/src/test/scala/protocbridge/frontend/PosixPluginFrontendSpec.scala b/bridge/src/test/scala/protocbridge/frontend/PosixPluginFrontendSpec.scala index 0d6d76a..2a3481c 100644 --- a/bridge/src/test/scala/protocbridge/frontend/PosixPluginFrontendSpec.scala +++ b/bridge/src/test/scala/protocbridge/frontend/PosixPluginFrontendSpec.scala @@ -3,7 +3,11 @@ package protocbridge.frontend class PosixPluginFrontendSpec extends OsSpecificFrontendSpec { if (!PluginFrontend.isWindows) { it must "execute a program that forwards input and output to given stream" in { - testPluginFrontend(PosixPluginFrontend) + testSuccess(PosixPluginFrontend) + } + + it must "not hang if there is an OOM in generator" in { + testFailure(PosixPluginFrontend) } } } diff --git a/bridge/src/test/scala/protocbridge/frontend/WindowsPluginFrontendSpec.scala b/bridge/src/test/scala/protocbridge/frontend/WindowsPluginFrontendSpec.scala index 7936141..4bf39de 100644 --- a/bridge/src/test/scala/protocbridge/frontend/WindowsPluginFrontendSpec.scala +++ b/bridge/src/test/scala/protocbridge/frontend/WindowsPluginFrontendSpec.scala @@ -3,7 +3,11 @@ package protocbridge.frontend class WindowsPluginFrontendSpec extends OsSpecificFrontendSpec { if (PluginFrontend.isWindows) { it must "execute a program that forwards input and output to given stream" in { - testPluginFrontend(WindowsPluginFrontend) + testSuccess(WindowsPluginFrontend) + } + + it must "not hang if there is an OOM in generator" in { + testFailure(WindowsPluginFrontend) } } }