Skip to content

Commit

Permalink
Avoid potential race condition edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 10, 2024
1 parent 4c476ac commit ff9bb48
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions wrapper/src/main/java/dev/lukebemish/immaculate/wrapper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,23 @@ private static void logException(Throwable t) {
}

private record Output(DataOutputStream output) {
synchronized void writeFailure(int id) throws IOException {
output.writeInt(id);
output.writeBoolean(false);
output.flush();
void writeFailure(int id) throws IOException {
synchronized (this) {
output.writeInt(id);
output.writeBoolean(false);
output.flush();
}
}

synchronized void writeSuccess(int id, String result) throws IOException {
output.writeInt(id);
output.writeBoolean(true);
byte[] bytes = result.getBytes(StandardCharsets.UTF_8);
output.writeInt(bytes.length);
output.write(bytes);
output.flush();
void writeSuccess(int id, String result) throws IOException {
synchronized (this) {
output.writeInt(id);
output.writeBoolean(true);
byte[] bytes = result.getBytes(StandardCharsets.UTF_8);
output.writeInt(bytes.length);
output.write(bytes);
output.flush();
}
}
}
}

0 comments on commit ff9bb48

Please sign in to comment.