From: AsamK Date: Thu, 5 Sep 2019 08:15:52 +0000 (+0200) Subject: Synchronize fileChannel access X-Git-Tag: v0.6.3~5 X-Git-Url: https://git.nmode.ca/signal-cli/commitdiff_plain/e36a54e7ccb7e103027c71c6128854f06537d787?ds=inline Synchronize fileChannel access Potention fix for #89 --- diff --git a/src/main/java/org/asamk/signal/storage/SignalAccount.java b/src/main/java/org/asamk/signal/storage/SignalAccount.java index 4378d052..6af9d460 100644 --- a/src/main/java/org/asamk/signal/storage/SignalAccount.java +++ b/src/main/java/org/asamk/signal/storage/SignalAccount.java @@ -129,7 +129,11 @@ public class SignalAccount { } private void load() throws IOException { - JsonNode rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel)); + JsonNode rootNode; + synchronized (fileChannel) { + fileChannel.position(0); + rootNode = jsonProcessor.readTree(Channels.newInputStream(fileChannel)); + } JsonNode node = rootNode.get("deviceId"); if (node != null) { @@ -204,10 +208,12 @@ public class SignalAccount { .putPOJO("threadStore", threadStore) ; try { - fileChannel.position(0); - jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode); - fileChannel.truncate(fileChannel.position()); - fileChannel.force(false); + synchronized (fileChannel) { + fileChannel.position(0); + jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode); + fileChannel.truncate(fileChannel.position()); + fileChannel.force(false); + } } catch (Exception e) { System.err.println(String.format("Error saving file: %s", e.getMessage())); }