diff --git a/src/main/java/org/qortal/controller/Controller.java b/src/main/java/org/qortal/controller/Controller.java index 1f44d622..4d3ad391 100644 --- a/src/main/java/org/qortal/controller/Controller.java +++ b/src/main/java/org/qortal/controller/Controller.java @@ -870,6 +870,17 @@ public class Controller extends Thread { // We were interrupted while waiting for thread to join } + // Make sure we're the only thread modifying the blockchain when shutting down the repository + ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock(); + try { + if (!blockchainLock.tryLock(5, TimeUnit.SECONDS)) { + LOGGER.debug("Couldn't acquire blockchain lock even after waiting 5 seconds"); + // Proceed anyway, as we have to shut down + } + } catch (InterruptedException e) { + LOGGER.info("Interrupted when waiting for blockchain lock"); + } + try { LOGGER.info("Shutting down repository"); RepositoryManager.closeRepositoryFactory(); @@ -877,6 +888,11 @@ public class Controller extends Thread { LOGGER.error("Error occurred while shutting down repository", e); } + // Release the lock if we acquired it + if (blockchainLock.isHeldByCurrentThread()) { + blockchainLock.unlock(); + } + LOGGER.info("Shutting down NTP"); NTP.shutdownNow();