Skip to content

Commit

Permalink
More logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bell-db committed Aug 9, 2024
1 parent 86d9d17 commit 0071ad0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
19 changes: 11 additions & 8 deletions bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ object PluginFrontend {
gen: ProtocCodeGenerator,
request: Array[Byte]
): Array[Byte] = {
gen.run(request)
// Use try-catch to handle all Throwable including OutOfMemoryError, StackOverflowError, etc.
try {
gen.run(request)
} catch {
case throwable: Throwable =>
System.err.println("createCodeGeneratorResponseWithError...")
createCodeGeneratorResponseWithError(
throwable.toString + "\n" + getStackTrace(throwable)
)
}
}

def createCodeGeneratorResponseWithError(error: String): Array[Byte] = {
Expand Down Expand Up @@ -110,17 +119,11 @@ object PluginFrontend {
gen: ProtocCodeGenerator,
fsin: InputStream,
env: ExtraEnv
): Array[Byte] = try {
): Array[Byte] = {
System.err.println("readInputStreamToByteArrayWithEnv...")
val bytes = readInputStreamToByteArrayWithEnv(fsin, env)
System.err.println("runWithBytes...")
runWithBytes(gen, bytes)
} catch {
case throwable: Throwable =>
System.err.println("createCodeGeneratorResponseWithError...")
createCodeGeneratorResponseWithError(
throwable.toString + "\n" + getStackTrace(throwable)
)
}

def createTempFile(extension: String, content: String): Path = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,42 @@ object PosixPluginFrontend extends PluginFrontend {

Future {
blocking {
System.err.println("Files.newInputStream...")
val fsin = Files.newInputStream(inputPipe)
System.err.println("PluginFrontend.runWithInputStream...")
val response = PluginFrontend.runWithInputStream(plugin, fsin, env)
System.err.println("fsin.close...")
fsin.close()
try {
System.err.println("Files.newInputStream...")
val fsin = Files.newInputStream(inputPipe)
System.err.println("PluginFrontend.runWithInputStream...")
val response = PluginFrontend.runWithInputStream(plugin, fsin, env)
System.err.println("fsin.close...")
fsin.close()

System.err.println("Files.newOutputStream...")
val fsout = Files.newOutputStream(outputPipe)
System.err.println("fsout.write...")
fsout.write(response)
System.err.println("fsout.close...")
fsout.close()
System.err.println("Files.newOutputStream...")
val fsout = Files.newOutputStream(outputPipe)
System.err.println("fsout.write...")
fsout.write(response)
System.err.println("fsout.close...")
fsout.close()

System.err.println("blocking done.")
System.err.println("blocking done.")
} catch {
case e: Throwable =>
// Handles rare exceptions not already gracefully handled in `runWithBytes`.
// Such exceptions aren't converted to `CodeGeneratorResponse`
// because `fsin` might not be fully consumed,
// therefore the plugin shell script might hang on `inputPipe`,
// and never consume `CodeGeneratorResponse`.
System.err.println("Exception occurred in PluginFrontend outside runWithBytes")
e.printStackTrace(System.err)
// Force an exit of the program.
// This is because the plugin shell script might hang on `inputPipe`,
// due to `fsin` not fully consumed.
// Or it might hang on `outputPipe`, due to `fsout` not closed.
// We can't simply close `fsout` here either,
// because `Files.newOutputStream(outputPipe)` will hang
// if `outputPipe` is not yet opened by the plugin shell script for reading.
// Therefore, the program might be stuck waiting for protoc,
// which in turn is waiting for the plugin shell script.
sys.exit(1)
}
}
}
(sh, InternalState(inputPipe, outputPipe, tempDirPath, sh))
Expand Down

0 comments on commit 0071ad0

Please sign in to comment.