Skip to content

Concurrent AssumeUTXO snapshot loads collide on the database lock #37064

Backlog

Description

@l0rinc

Two simultaneous loadtxoutset calls can both pass the check for an already active snapshot chainstate, then both shrink the caches and open the same LevelDB directory. One import succeeds, and the other fails on the held chainstate_snapshot lock instead of with the usual already-active error.

Reproducer
diff --git a/test/functional/feature_assumeutxo.py b/test/functional/feature_assumeutxo.py
index fa9466f2d8..8e38dc70a8 100755
--- a/test/functional/feature_assumeutxo.py
+++ b/test/functional/feature_assumeutxo.py
@@ -11,6 +11,7 @@ The assumeutxo value generated and used here is committed to in
 """
 import contextlib
 
+from concurrent.futures import ThreadPoolExecutor
 from dataclasses import dataclass
 from test_framework.blocktools import (
         create_block,
@@ -32,6 +33,7 @@ from test_framework.p2p import (
 )
 from test_framework.test_framework import BitcoinTestFramework
 from test_framework.util import (
+    JSONRPCException,
     assert_approx,
     assert_equal,
     assert_not_equal,
@@ -391,6 +393,29 @@ class AssumeutxoTest(BitcoinTestFramework):
 
         assert not (node.datadir_path / node.chain / "chainstate_snapshot").exists()
 
+    def test_concurrent_snapshot_load(self, snapshot_path):
+        self.log.info("Test loading a snapshot from two concurrent RPC calls")
+        node = self.nodes[2]
+        assert_equal(node.getblockcount(), START_HEIGHT)
+
+        def load_snapshot(_):
+            try:
+                return "ok", node.cli.send_cli("loadtxoutset", str(snapshot_path))
+            except JSONRPCException as e:
+                return "error", str(e)
+
+        with ThreadPoolExecutor(max_workers=2) as executor:
+            results = list(executor.map(load_snapshot, range(2)))
+
+        assert_equal(len([result for result in results if result[0] == "ok"]), 1)
+        errors = [result[1] for result in results if result[0] == "error"]
+        assert_equal(len(errors), 1)
+        assert "already held by process" in errors[0]  # TODO: Concurrent activation must behave like a sequential second call
+
+        self.stop_node(2)
+        self.cleanup_folder(node.chain_path / "chainstate_snapshot")
+        self.start_node(2)
+
     def run_test(self):
         """
         Bring up two (disconnected) nodes, mine some new blocks on the first,
@@ -515,6 +540,7 @@ class AssumeutxoTest(BitcoinTestFramework):
         self.test_invalid_snapshot_scenarios(dump_output['path'])
         self.test_invalid_chainstate_scenarios()
         self.test_invalid_file_path()
+        self.test_concurrent_snapshot_load(dump_output['path'])
         self.test_snapshot_block_invalidated(dump_output['path'])
         self.test_snapshot_not_on_most_work_chain(dump_output['path'])
 

The direct fix would allow only one snapshot activation at a time, but given that runtime snapshot import has several independent races (see also #37056), I'm proposing to remove AssumeUTXO entirely in #36598 instead.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions