Skip to content

RocksDbStorage metadata counters have read-modify-write race conditions #1728

@huitseeker

Description

@huitseeker

The insert_value and remove_value methods perform read-modify-write operations on leaf_count and entry_count metadata without proper synchronization. Concurrent modifications can cause these counters to become corrupted through lost updates.

The root cause is using separate read and write operations instead of atomic transactions. In miden-crypto/src/merkle/smt/large/storage/rocksdb.rs:318-360, the code reads current counts (lines 318-319), modifies them locally (lines 335, 345-347), and writes them back in a batch (lines 356-357). If two threads execute concurrently:

  1. Thread A reads entry_count = 5
  2. Thread B reads entry_count = 5
  3. Thread A increments to 6 and writes
  4. Thread B increments to 6 and writes (should be 7)

Result: entry_count = 6 instead of the correct value 7.

Fixing this requires investigation of RocksDB's synchronization and concurrency primitives to determine the right approach. Options include merge operators for atomic counter updates, compare-and-swap operations, RocksDB transactions, or external synchronization if the storage is shared across threads. The investigation should clarify the intended concurrency model and select appropriate RocksDB features.

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