Skip to content

Commit

Permalink
Fixed the kernel shutdown hanging due to the synchronized lock
Browse files Browse the repository at this point in the history
  • Loading branch information
AionJayT committed Mar 18, 2020
1 parent 12f8c0d commit 24ecb9f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.EnumMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import org.aion.log.LogUtil;
import org.aion.zero.impl.blockchain.AionHub.BestBlockImportCallback;
Expand Down Expand Up @@ -204,6 +205,7 @@ public class AionBlockchainImpl implements IAionBlockchain {
private SelfNodeStatusCallback callback;
private BestBlockImportCallback bestBlockCallback;
ReentrantLock blockChainCoreLock = new ReentrantLock();
private AtomicBoolean shutDownFlag = new AtomicBoolean();

/**
* The constructor for the blockchain initialization {@see AionHub}.
Expand Down Expand Up @@ -921,14 +923,19 @@ public ImportResult tryToConnect(final Block block) {
public ImportResult tryToConnect(final BlockWrapper blockWrapper) {
blockChainCoreLock.lock();
try {
checkKernelShutdownForCLI();
return tryToConnectWithTimedExecution(blockWrapper).getLeft();
if (!checkKernelShutdownForCLI()) {
return tryToConnectWithTimedExecution(blockWrapper).getLeft();
} else {
// Return a result and then shutdown the kernel in the finally condition
return ImportResult.KERNEL_EXIT;
}
} finally{
blockChainCoreLock.unlock();
checkKernelExit();
}
}

private void checkKernelShutdownForCLI() {
private boolean checkKernelShutdownForCLI() {
if (bestBlock.getNumber() == shutdownHook) {
LOG.info("Shutting down and dumping heap as indicated by CLI request since block number {} was reached.", shutdownHook);

Expand All @@ -938,12 +945,14 @@ private void checkKernelShutdownForCLI() {
LOG.error("Unable to dump heap due to exception:", e);
}

// requested shutdown
System.exit(SystemExitCodes.NORMAL);
shutDownFlag.set(true);
return true;
} else if (enableFullSyncCheck && reachedFullSync) {
LOG.info("Shutting down as indicated by CLI request sync to the top {} was reached.", bestBlock.getNumber());
System.exit(SystemExitCodes.NORMAL);
shutDownFlag.set(true);
return true;
}
return false;
}

/**
Expand All @@ -965,7 +974,9 @@ public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List
ImportResult importResult = null;
Set<ByteArrayWrapper> imported = new HashSet<>();
for (Block block : blockRange) {
checkKernelShutdownForCLI();
if (checkKernelShutdownForCLI()) {
break;
}

Pair<ImportResult, Long> result = tryToConnectWithTimedExecution(new BlockWrapper(block));
importResult = result.getLeft();
Expand Down Expand Up @@ -994,6 +1005,7 @@ public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List
return Triple.of(bestBlock.getNumber(), imported, importResult);
} finally{
blockChainCoreLock.unlock();
checkKernelExit();
}
}

Expand Down Expand Up @@ -2746,4 +2758,10 @@ public void load(AionGenesis genesis, Logger genLOG) {
}
}
}

private void checkKernelExit() {
if (shutDownFlag.get()) {
System.exit(SystemExitCodes.NORMAL);
}
}
}
3 changes: 2 additions & 1 deletion modAionImpl/src/org/aion/zero/impl/core/ImportResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public enum ImportResult {
EXIST,
NO_PARENT,
INVALID_BLOCK,
CONSENSUS_BREAK;
CONSENSUS_BREAK,
KERNEL_EXIT;

public boolean isSuccessful() {
return equals(IMPORTED_BEST) || equals(IMPORTED_NOT_BEST);
Expand Down

0 comments on commit 24ecb9f

Please sign in to comment.