-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Acquire cs_main lock before cs_wallet during wallet initialization #11126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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/
cea5319
to
fef5c2c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Tested ACK fef5c2c.
One nit inline.
@@ -3107,6 +3107,8 @@ CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_c | |||
|
|||
DBErrors CWallet::LoadWallet(bool& fFirstRunRet) | |||
{ | |||
LOCK2(cs_main, cs_wallet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. You can remove the LOCK(cs_wallet);
from further down in this function (currently L3118)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 09ee3fa
fef5c2c
to
de9a1db
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added commit fef5c2c -> 09ee3fa (pr/loadlock2.3 -> pr/loadlock2.4, compare)
Squashed 09ee3fa -> de9a1db (pr/loadlock2.4 -> pr/loadlock2.5)
@@ -3107,6 +3107,8 @@ CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, const CCoinControl& coin_c | |||
|
|||
DBErrors CWallet::LoadWallet(bool& fFirstRunRet) | |||
{ | |||
LOCK2(cs_main, cs_wallet); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 09ee3fa
Tested ACK de9a1db |
utACK de9a1db |
…tialization de9a1db Acquire cs_main lock before cs_wallet during wallet initialization (Russell Yanofsky) Pull request description: `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. Error was reported by @luke-jr in https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/ Tree-SHA512: 353fe21bc0a4a2828b41876897001a3c414d4b115ee7430925bd391d8bc396fca81661145d00996c1ba1a01516d9acf8b89fb5c3da27092f5f3aa7e37ef26ffa
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/ Github-Pull: bitcoin#11126 Rebased-From: de9a1db
…let initialization de9a1db Acquire cs_main lock before cs_wallet during wallet initialization (Russell Yanofsky) Pull request description: `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. Error was reported by @luke-jr in https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/ Tree-SHA512: 353fe21bc0a4a2828b41876897001a3c414d4b115ee7430925bd391d8bc396fca81661145d00996c1ba1a01516d9acf8b89fb5c3da27092f5f3aa7e37ef26ffa
…let initialization de9a1db Acquire cs_main lock before cs_wallet during wallet initialization (Russell Yanofsky) Pull request description: `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. Error was reported by @luke-jr in https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/ Tree-SHA512: 353fe21bc0a4a2828b41876897001a3c414d4b115ee7430925bd391d8bc396fca81661145d00996c1ba1a01516d9acf8b89fb5c3da27092f5f3aa7e37ef26ffa
…let initialization de9a1db Acquire cs_main lock before cs_wallet during wallet initialization (Russell Yanofsky) Pull request description: `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. Error was reported by @luke-jr in https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/ Tree-SHA512: 353fe21bc0a4a2828b41876897001a3c414d4b115ee7430925bd391d8bc396fca81661145d00996c1ba1a01516d9acf8b89fb5c3da27092f5f3aa7e37ef26ffa
…llet initialization 675ab11 Acquire cs_main lock before cs_wallet during wallet initialization (random-zebra) Pull request description: Fixing ``` 2020-08-01 09:53:49 POTENTIAL DEADLOCK DETECTED 2020-08-01 09:53:49 Previous lock order was: 2020-08-01 09:53:49 (1) cs_main wallet/wallet.cpp:1617 (in thread ) 2020-08-01 09:53:49 (2) cs_wallet wallet/wallet.cpp:1617 (in thread ) 2020-08-01 09:53:49 Current lock order is: 2020-08-01 09:53:49 (2) pwallet->cs_wallet wallet/walletdb.cpp:708 (in thread ) 2020-08-01 09:53:49 (1) cs_main wallet/wallet.cpp:1013 (in thread ) ``` (backports bitcoin#11126) > `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. ACKs for top commit: furszy: nice finding, ACK 675ab11 . Fuzzbawls: ACK 675ab11 Tree-SHA512: 8f802101bea4b352b52177d79c4c3d2e22666b6859735e27b40f8adbfeaa21acbf0dc8668c60dd50590c165c77532733fc91630edba4be4a118f00b1946205ae
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/ zcash: cherry picked from commit de9a1db zcash: bitcoin/bitcoin#11126
Bitcoin 0.16 locking PRs These are locking changes from upstream (bitcoin core) release 0.16 (Aug 14, 2017, to Feb 16, 2018), oldest to newest (when merged to the master branch). Each commit also includes a reference both to the PR and the upstream commit. - bitcoin/bitcoin#11126 - Excludes changes to wallet tests that we don't have. - bitcoin/bitcoin#10916 - first commit only; second commit already merged by d9fcc2b - bitcoin/bitcoin#11107 - Only the last commit. - bitcoin/bitcoin#11593 - bitcoin/bitcoin#11585 - bitcoin/bitcoin#11618 - bitcoin/bitcoin#10286 - Only the third and last commits. - bitcoin/bitcoin#11870 - bitcoin/bitcoin#12330 - bitcoin/bitcoin#12366 - bitcoin/bitcoin#12368 - bitcoin/bitcoin#12333 - Only the first commit.
CWallet::MarkConflicted
may acquire thecs_main
lock afterCWalletDB::LoadWallet
acquires thecs_wallet
lock during wallet initialization. (CWalletDB::LoadWallet
callsReadKeyValue
which callsCWallet::LoadToWallet
which callsCWallet::MarkConflicted
). This is the opposite order thatcs_main
andcs_wallet
locks are acquired in the rest of the code, and so leads toPOTENTIAL DEADLOCK DETECTED
errors if bitcoin is built with-DDEBUG_LOCKORDER
.This commit changes
CWallet::LoadWallet
(which callsCWalletDB::LoadWallet
) to acquire both locks in the standard order.Error was reported by @luke-jr in https://botbot.me/freenode/bitcoin-core-dev/msg/90244330/