Skip to content

Commit

Permalink
Avoid some more potential deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 10, 2024
1 parent ff9bb48 commit a9e1ebe
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/main/java/dev/lukebemish/immaculate/ForkFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,25 @@ private void beginClose(Throwable e) throws IOException {
}

private void finishClose() throws IOException {
output.writeInt(-1);
socket.close();
if (!socket.isClosed()) {
output.writeInt(-1);
output.flush();
socket.close();
}
}

public void shutdown() throws IOException {
shutdown(new IOException("Execution was interrupted"));
}

private void shutdown(Throwable t) throws IOException {
private synchronized void shutdown(Throwable t) throws IOException {
this.beginClose(t);
try {
this.join();
} catch (InterruptedException e) {
// continue, it's fine
if (Thread.currentThread() != this) {
try {
this.join();
} catch (InterruptedException e) {
// continue, it's fine
}
}
this.finishClose();
}
Expand Down Expand Up @@ -195,8 +200,12 @@ public void run() {
}
}
}
} catch (EOFException ignored) {

} catch (EOFException e) {
try {
shutdown(e);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down

0 comments on commit a9e1ebe

Please sign in to comment.