Skip to content

Review finding: listwalletdir drops the exclusive lock on loaded SQLite wallets #280

Closed

Description

@kiwidream

Summary

listwalletdir, and the Open Wallet menu in qbit-qt, release the process's exclusive SQLite lock on every loaded wallet. A second process can then load a live wallet. With two writers, PQC counter updates are lost and the wallet database is corrupted. The code is unchanged from Bitcoin Core v30.2.

Observed

On POSIX, closing any file descriptor for a file drops every fcntl lock the process holds on that file. ListDatabases calls IsBDBFile and IsSQLiteFile, which open wallet.dat with std::ifstream and close it (src/wallet/db.cpp:23-61,96-147). SQLite in exclusive locking mode (src/wallet/sqlite.cpp:281-291) takes the lock once and does not re-check it.

On regtest at 1.x.x e374ba24cf, with two qbitd processes sharing -walletdir:

  • Before node0 runs listwalletdir, loadwallet on node1 fails with "Unable to obtain an exclusive lock". After it, the same load succeeds.
  • node0 signs twice (counter 5) and node1 signs once (counter 4). Six signatures were made, but the counter on disk is 5.
  • PRAGMA integrity_check then reports "Rowid … out of order" and "wrong # of entries in index", and reloading fails with "Failed to verify database".
  • With -unsafesqlitesync=0, a second qbitd that only ran loadwallet and then unloadwallet on the live file left "Freelist: invalid page number …", and the owner could not reload the wallet.
  • qbit-wallet info and qbit-wallet dump on the live wallet are refused before listwalletdir and succeed after it.

A failed duplicate loadwallet of the same name does not drop the lock. Loading the same file in-process under a different path also reaches IsSQLiteFile, through MakeDatabase (src/wallet/walletdb.cpp:1668). qbit-qt calls listWalletDir() when the Open Wallet menu is shown (src/qt/bitcoingui.cpp:436,501).

Impact

Low. It needs a local operator mistake: two nodes sharing a wallet directory, a symlink, or loadwallet with an absolute path. In Core the result is a corrupted wallet. In qbit it also splits bounded30 counters, because each process reserves from its own in-memory counter (src/wallet/scriptpubkeyman.cpp:2315-2384). The "keep only one live signer copy" guidance in doc/user/wallet/p2mr-wallets.md assumes the lock holds. POSIX only; Windows was not checked. The existing lock tests (test/functional/wallet_multiwallet.py:404-408, test/functional/tool_wallet.py:150) never call listwalletdir first.

Suggested fix

Do not probe with ifstream a file the process may hold. Any one of these works:

  • Skip paths whose (dev, inode) matches a loaded wallet.
  • Probe through sqlite3_open_v2(..., SQLITE_OPEN_READONLY), whose unix VFS defers the close while the process holds locks on the file.
  • Hold an F_OFD_SETLK lock on a sidecar lock file for each wallet's lifetime.

Add a regression test: the owner runs listwalletdir, then a second-process loadwallet must still fail. Since the code is inherited, an upstream report may also be warranted.

Evidence

src/wallet/db.cpp:23-61,96-147; src/wallet/sqlite.cpp:281-299; src/wallet/walletdb.cpp:1668; src/qt/bitcoingui.cpp:436,501. git diff of src/wallet/db.cpp and src/wallet/sqlite.cpp against Bitcoin Core v30.2 is empty.

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