Skip to content

Commit 6d1cfd0

Browse files
committed
mid work
1 parent 1fb7ec8 commit 6d1cfd0

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/@jsii/java-runtime/project/src/main/java/software/amazon/jsii/JsiiRuntime.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,10 @@ private void acceptData(final boolean uninterruptible) throws IOException {
516516
this.buffer.write(read);
517517
}
518518
if (read == '\n' || this.eof) {
519-
processLine(new String(buffer.toByteArray(), StandardCharsets.UTF_8));
519+
// don't print an empty line if we're at the end of the file and the buffer is empty.
520+
if (!this.eof || buffer.size() > 0) {
521+
processLine(new String(buffer.toByteArray(), StandardCharsets.UTF_8));
522+
}
520523
buffer.reset();
521524
}
522525
}
@@ -526,6 +529,13 @@ private void processLine(final String line) {
526529
try {
527530
final JsonNode tree = objectMapper.readTree(line);
528531
final ConsoleOutput consoleOutput = objectMapper.treeToValue(tree, ConsoleOutput.class);
532+
if (consoleOutput == null) {
533+
// null means the line was empty, but the above objectMapper calls will return null
534+
// We would throw JsonProcessingException to avoid duplication, but the constructors
535+
// are protected, and the subclasses need too much information
536+
System.err.println(line);
537+
return;
538+
}
529539
if (consoleOutput.stderr != null) {
530540
System.err.write(consoleOutput.stderr, 0, consoleOutput.stderr.length);
531541
}

packages/@jsii/java-runtime/project/src/test/java/software/amazon/jsii/JsiiRuntimeTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99
import java.util.Collections;
1010

1111
public final class JsiiRuntimeTest {
12+
13+
@Test
14+
public void errorSteamSinkDoesntCrash() {
15+
16+
ByteArrayOutputStream err = new ByteArrayOutputStream();
17+
PrintStream originalErr = System.err;
18+
19+
// to capture writes to System.err, which happen when the error stream
20+
// sink crashes. kind of a silly...but simple, and works.
21+
System.setErr(new PrintStream(err));
22+
23+
try {
24+
final JsiiRuntime runtime = new JsiiRuntime(null, null);
25+
runtime.getClient().createObject("Object", Collections.emptyList(), Collections.emptyList(),
26+
Collections.emptyList());
27+
runtime.terminate();
28+
Assertions.assertFalse(err.toString().contains(
29+
"Unexpected error in background thread \"software.amazon.jsii.JsiiRuntime.ErrorStreamSink\""));
30+
} finally {
31+
System.setErr(originalErr);
32+
}
33+
}
34+
1235
@Test
1336
public void withNoCustomization() {
1437
final JsiiRuntime runtime = new JsiiRuntime(null, null);

0 commit comments

Comments
 (0)