diff --git a/src/validation.cpp b/src/validation.cpp
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3126,6 +3126,7 @@
 
 bool FinalizeBlockAndInvalidate(const Config &config, CValidationState &state,
                                 CBlockIndex *pindex) {
+    CBlockIndex *pindexToInvalidate;
     {
         LOCK(cs_main);
         if (!FinalizeBlockInternal(config, state, pindex)) {
@@ -3137,16 +3138,16 @@
         if (pindex->nStatus.isOnParkedChain()) {
             UnparkBlock(pindex);
         }
-    }
 
-    // If the finalized block is not on the active chain, we need to rewind.
-    if (AreOnTheSameFork(pindex, chainActive.Tip())) {
-        return true;
-    }
+        // If the finalized block is not on the active chain, we need to rewind.
+        if (AreOnTheSameFork(pindex, chainActive.Tip())) {
+            return true;
+        }
 
-    const CBlockIndex *pindexFork = chainActive.FindFork(pindex);
-    CBlockIndex *pindexToInvalidate =
-        chainActive.Tip()->GetAncestor(pindexFork->nHeight + 1);
+        const CBlockIndex *pindexFork = chainActive.FindFork(pindex);
+        pindexToInvalidate =
+            chainActive.Tip()->GetAncestor(pindexFork->nHeight + 1);
+    }
 
     return InvalidateBlock(config, state, pindexToInvalidate);
 }