Skip to content

Commit 7af2d2f

Browse files
Merge pull request #85 from vcase/fix-bazel-query-hang
Fix bazel query hang in the case where bazel writes to stderr
2 parents 407ba7b + bf2a1ec commit 7af2d2f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/main/java/com/bazel_diff/BazelClient.java

+24
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,35 @@ private List<Build.Target> performBazelQuery(String query) throws IOException {
104104
ProcessBuilder pb = new ProcessBuilder(cmd).directory(workingDirectory.toFile());
105105
Process process = pb.start();
106106
ArrayList<Build.Target> targets = new ArrayList<>();
107+
108+
// Prevent process hang in the case where bazel writes to stderr.
109+
// See https://stackoverflow.com/questions/3285408/java-processbuilder-resultant-process-hangs
110+
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
111+
Thread tStdError = new Thread(new Runnable() {
112+
String line = null;
113+
public void run() {
114+
try {
115+
while ((line = stdError.readLine()) != null) {
116+
if (verbose) {
117+
System.out.println(line);
118+
}
119+
120+
if(Thread.currentThread().isInterrupted()) {
121+
return;
122+
}
123+
}
124+
} catch(IOException e) {}
125+
}
126+
});
127+
tStdError.start();
128+
107129
while (true) {
108130
Build.Target target = Build.Target.parseDelimitedFrom(process.getInputStream());
109131
if (target == null) break; // EOF
110132
targets.add(target);
111133
}
134+
135+
tStdError.interrupt();
112136

113137
Files.delete(tempFile);
114138

0 commit comments

Comments
 (0)