Skip to content

Commit 5087896

Browse files
Don't close process.getInputStream() on failure (#6541)
1 parent 2f363ee commit 5087896

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ public static String readFully(InputStream inputStream, Charset charset) {
221221
}
222222
}
223223

224+
public static String readFullyWithoutClosing(InputStream is, Charset charset) {
225+
try {
226+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
227+
byte[] buffer = new byte[4096];
228+
int n;
229+
while ((n = is.read(buffer)) != -1) {
230+
bos.write(buffer, 0, n);
231+
}
232+
233+
byte[] bytes = bos.toByteArray();
234+
return new String(bytes, charset);
235+
} catch (IOException e) {
236+
throw new UnsupportedOperationException(e);
237+
}
238+
}
239+
224240
public static String capitalize(String value) {
225241
if (value.isEmpty()) {
226242
return value;

rewrite-core/src/main/java/org/openrewrite/rpc/RewriteRpcProcess.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
import java.io.IOException;
3535
import java.io.InputStream;
3636
import java.io.UncheckedIOException;
37+
import java.nio.charset.StandardCharsets;
3738
import java.nio.file.Path;
3839
import java.nio.file.Paths;
3940
import java.util.LinkedHashMap;
4041
import java.util.Map;
4142
import java.util.concurrent.TimeUnit;
4243

4344
import static org.openrewrite.internal.StringUtils.readFully;
45+
import static org.openrewrite.internal.StringUtils.readFullyWithoutClosing;
4446

4547
/**
4648
* A client for spawning and communicating with a subprocess that implements Rewrite RPC.
@@ -95,18 +97,16 @@ public void run() {
9597
public @Nullable RuntimeException getLivenessCheck() {
9698
if (process != null && !process.isAlive()) {
9799
int exitCode = process.exitValue();
98-
String errorOutput = "", stdOutput = "";
100+
String message = "JavaScript RPC process shut down early with exit code " + exitCode;
99101

100-
// Read any remaining output from the process
101-
try (InputStream errorStream = process.getErrorStream();
102-
InputStream inputStream = process.getInputStream()) {
103-
errorOutput = readFully(errorStream);
104-
stdOutput = readFully(inputStream);
105-
} catch (IOException | UnsupportedOperationException e) {
106-
// Ignore errors reading final output
102+
String errorOutput = "", stdOutput = "";
103+
try {
104+
errorOutput = readFullyWithoutClosing(process.getErrorStream(), StandardCharsets.UTF_8);
105+
stdOutput = readFullyWithoutClosing(process.getInputStream(), StandardCharsets.UTF_8);
106+
} catch (UnsupportedOperationException e) {
107+
message += "\nError retrieving output/error stream: " + e.getMessage();
107108
}
108109

109-
String message = "JavaScript RPC process shut down early with exit code " + exitCode;
110110
if (!stdOutput.isEmpty()) {
111111
message += "\nStandard output:\n " + stdOutput.replace("\n", "\n ");
112112
}

0 commit comments

Comments
 (0)