Skip to content

Commit 0071ad0

Browse files
committed
More logging
1 parent 86d9d17 commit 0071ad0

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

bridge/src/main/scala/protocbridge/frontend/PluginFrontend.scala

+11-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ object PluginFrontend {
4747
gen: ProtocCodeGenerator,
4848
request: Array[Byte]
4949
): Array[Byte] = {
50-
gen.run(request)
50+
// Use try-catch to handle all Throwable including OutOfMemoryError, StackOverflowError, etc.
51+
try {
52+
gen.run(request)
53+
} catch {
54+
case throwable: Throwable =>
55+
System.err.println("createCodeGeneratorResponseWithError...")
56+
createCodeGeneratorResponseWithError(
57+
throwable.toString + "\n" + getStackTrace(throwable)
58+
)
59+
}
5160
}
5261

5362
def createCodeGeneratorResponseWithError(error: String): Array[Byte] = {
@@ -110,17 +119,11 @@ object PluginFrontend {
110119
gen: ProtocCodeGenerator,
111120
fsin: InputStream,
112121
env: ExtraEnv
113-
): Array[Byte] = try {
122+
): Array[Byte] = {
114123
System.err.println("readInputStreamToByteArrayWithEnv...")
115124
val bytes = readInputStreamToByteArrayWithEnv(fsin, env)
116125
System.err.println("runWithBytes...")
117126
runWithBytes(gen, bytes)
118-
} catch {
119-
case throwable: Throwable =>
120-
System.err.println("createCodeGeneratorResponseWithError...")
121-
createCodeGeneratorResponseWithError(
122-
throwable.toString + "\n" + getStackTrace(throwable)
123-
)
124127
}
125128

126129
def createTempFile(extension: String, content: String): Path = {

bridge/src/main/scala/protocbridge/frontend/PosixPluginFrontend.scala

+34-13
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,42 @@ object PosixPluginFrontend extends PluginFrontend {
3636

3737
Future {
3838
blocking {
39-
System.err.println("Files.newInputStream...")
40-
val fsin = Files.newInputStream(inputPipe)
41-
System.err.println("PluginFrontend.runWithInputStream...")
42-
val response = PluginFrontend.runWithInputStream(plugin, fsin, env)
43-
System.err.println("fsin.close...")
44-
fsin.close()
39+
try {
40+
System.err.println("Files.newInputStream...")
41+
val fsin = Files.newInputStream(inputPipe)
42+
System.err.println("PluginFrontend.runWithInputStream...")
43+
val response = PluginFrontend.runWithInputStream(plugin, fsin, env)
44+
System.err.println("fsin.close...")
45+
fsin.close()
4546

46-
System.err.println("Files.newOutputStream...")
47-
val fsout = Files.newOutputStream(outputPipe)
48-
System.err.println("fsout.write...")
49-
fsout.write(response)
50-
System.err.println("fsout.close...")
51-
fsout.close()
47+
System.err.println("Files.newOutputStream...")
48+
val fsout = Files.newOutputStream(outputPipe)
49+
System.err.println("fsout.write...")
50+
fsout.write(response)
51+
System.err.println("fsout.close...")
52+
fsout.close()
5253

53-
System.err.println("blocking done.")
54+
System.err.println("blocking done.")
55+
} catch {
56+
case e: Throwable =>
57+
// Handles rare exceptions not already gracefully handled in `runWithBytes`.
58+
// Such exceptions aren't converted to `CodeGeneratorResponse`
59+
// because `fsin` might not be fully consumed,
60+
// therefore the plugin shell script might hang on `inputPipe`,
61+
// and never consume `CodeGeneratorResponse`.
62+
System.err.println("Exception occurred in PluginFrontend outside runWithBytes")
63+
e.printStackTrace(System.err)
64+
// Force an exit of the program.
65+
// This is because the plugin shell script might hang on `inputPipe`,
66+
// due to `fsin` not fully consumed.
67+
// Or it might hang on `outputPipe`, due to `fsout` not closed.
68+
// We can't simply close `fsout` here either,
69+
// because `Files.newOutputStream(outputPipe)` will hang
70+
// if `outputPipe` is not yet opened by the plugin shell script for reading.
71+
// Therefore, the program might be stuck waiting for protoc,
72+
// which in turn is waiting for the plugin shell script.
73+
sys.exit(1)
74+
}
5475
}
5576
}
5677
(sh, InternalState(inputPipe, outputPipe, tempDirPath, sh))

0 commit comments

Comments
 (0)