HomePhabricator

Acquire cs_main lock before cs_wallet during wallet initialization

Description

Acquire cs_main lock before cs_wallet during wallet initialization

Summary:

CWallet::MarkConflicted may acquire the cs_main lock after
CWalletDB::LoadWallet acquires the cs_wallet lock during wallet
initialization.
(CWalletDB::LoadWallet calls ReadKeyValue which calls
CWallet::LoadToWallet
which calls CWallet::MarkConflicted). This is the opposite order that
cs_main
and cs_wallet locks are acquired in the rest of the code, and so leads
to
POTENTIAL DEADLOCK DETECTED errors if bitcoin is built with
-DDEBUG_LOCKORDER.

This commit changes CWallet::LoadWallet (which calls
CWalletDB::LoadWallet) to
acquire both locks in the standard order. It also fixes some tests that
were
acquiring wallet and main locks out of order and failed with the new
locking in
CWallet::LoadWallet.

Error was reported by Luke Dashjr <luke-jr@utopios.org> in
https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/

Backport of core PR11126

Test Plan:

../configure --enable-debug
make check

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: teamcity, schancel

Differential Revision: https://reviews.bitcoinabc.org/D2631

Details

Provenance
Russell Yanofsky <russ@yanofsky.org>Authored on Aug 24 2017, 18:12
FabienCommitted on Feb 28 2019, 16:40
jasonbcoxPushed on Mar 1 2019, 00:55
Reviewer
Restricted Project
Differential Revision
D2631: Acquire cs_main lock before cs_wallet during wallet initialization
Parents
rSTAGINGafadcafdac8f: tests: Add missing locks to tests
Branches
Unknown
Tags
Unknown