Merge pull request #4907 from chimp1984/avoid-persistence-write-call-before-read

Prevent that we write data at shutdown before we have read the data.
This commit is contained in:
Christoph Atteneder 2020-12-08 16:36:38 +01:00 committed by GitHub
commit 6adf8503df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -110,7 +110,11 @@ public class PersistenceManager<T extends PersistableEnvelope> {
// For Priority.HIGH data we want to write to disk in any case to be on the safe side if we might have missed // For Priority.HIGH data we want to write to disk in any case to be on the safe side if we might have missed
// a requestPersistence call after an important state update. Those are usually rather small data stores. // a requestPersistence call after an important state update. Those are usually rather small data stores.
// Otherwise we only persist if requestPersistence was called since the last persist call. // Otherwise we only persist if requestPersistence was called since the last persist call.
if (persistenceManager.source.flushAtShutDown || persistenceManager.persistenceRequested) { // We also check if we have called read already to avoid a very early write attempt before we have ever
// read the data, which would lead to a write of empty data
// (fixes https://github.com/bisq-network/bisq/issues/4844).
if (persistenceManager.readCalled.get() &&
(persistenceManager.source.flushAtShutDown || persistenceManager.persistenceRequested)) {
// We always get our completeHandler called even if exceptions happen. In case a file write fails // We always get our completeHandler called even if exceptions happen. In case a file write fails
// we still call our shutdown and count down routine as the completeHandler is triggered in any case. // we still call our shutdown and count down routine as the completeHandler is triggered in any case.
@ -184,6 +188,7 @@ public class PersistenceManager<T extends PersistableEnvelope> {
private Timer timer; private Timer timer;
private ExecutorService writeToDiskExecutor; private ExecutorService writeToDiskExecutor;
public final AtomicBoolean initCalled = new AtomicBoolean(false); public final AtomicBoolean initCalled = new AtomicBoolean(false);
public final AtomicBoolean readCalled = new AtomicBoolean(false);
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
@ -303,6 +308,8 @@ public class PersistenceManager<T extends PersistableEnvelope> {
return null; return null;
} }
readCalled.set(true);
File storageFile = new File(dir, fileName); File storageFile = new File(dir, fileName);
if (!storageFile.exists()) { if (!storageFile.exists()) {
return null; return null;