Skip to content

Commit f6e6292

Browse files
committed
Fix SpotBugs OS_OPEN_STREAM: wrap BufferedReader in try-with-resources
Wrap the BufferedReader used for CLI port detection in a try-with-resources block to guarantee stream closure on all exit paths (normal completion, exceptions, timeout). Fixes #17
1 parent 387e4a4 commit f6e6292

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

src/main/java/com/github/copilot/sdk/CopilotClient.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,27 +1031,28 @@ private ProcessInfo startCliServer() throws IOException, InterruptedException {
10311031
Integer detectedPort = null;
10321032
if (!options.isUseStdio()) {
10331033
// Wait for port announcement
1034-
BufferedReader reader = new BufferedReader(
1035-
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
1036-
Pattern portPattern = Pattern.compile("listening on port (\\d+)", Pattern.CASE_INSENSITIVE);
1037-
long deadline = System.currentTimeMillis() + 30000;
1038-
1039-
while (System.currentTimeMillis() < deadline) {
1040-
String line = reader.readLine();
1041-
if (line == null) {
1042-
throw new IOException("CLI process exited unexpectedly");
1043-
}
1034+
try (BufferedReader reader = new BufferedReader(
1035+
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
1036+
Pattern portPattern = Pattern.compile("listening on port (\\d+)", Pattern.CASE_INSENSITIVE);
1037+
long deadline = System.currentTimeMillis() + 30000;
1038+
1039+
while (System.currentTimeMillis() < deadline) {
1040+
String line = reader.readLine();
1041+
if (line == null) {
1042+
throw new IOException("CLI process exited unexpectedly");
1043+
}
10441044

1045-
Matcher matcher = portPattern.matcher(line);
1046-
if (matcher.find()) {
1047-
detectedPort = Integer.parseInt(matcher.group(1));
1048-
break;
1045+
Matcher matcher = portPattern.matcher(line);
1046+
if (matcher.find()) {
1047+
detectedPort = Integer.parseInt(matcher.group(1));
1048+
break;
1049+
}
10491050
}
1050-
}
10511051

1052-
if (detectedPort == null) {
1053-
process.destroyForcibly();
1054-
throw new IOException("Timeout waiting for CLI to announce port");
1052+
if (detectedPort == null) {
1053+
process.destroyForcibly();
1054+
throw new IOException("Timeout waiting for CLI to announce port");
1055+
}
10551056
}
10561057
}
10571058

0 commit comments

Comments
 (0)