Skip to content

Conversation

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 10, 2017

Add missing locks required when accessing:

int32_t nBlockSequenceId GUARDED_BY(cs_nBlockSequenceId) = 1;
int nLastBlockFile GUARDED_BY(cs_LastBlockFile) = 0;
bool fCheckForPruning GUARDED_BY(cs_LastBlockFile) = false;
CuckooCache::cache<uint256, SignatureCacheHasher> scriptExecutionCache GUARDED_BY(cs_main);
BlockMap& mapBlockIndex GUARDED_BY(cs_main);
std::unique_ptr<CCoinsViewDB> pcoinsdbview GUARDED_BY(cs_main);
std::unique_ptr<CCoinsViewCache> pcoinsTip GUARDED_BY(cs_main);
std::unique_ptr<CBlockTreeDB> pblocktree GUARDED_BY(cs_main);

Also, add the locking annotations that follow from the requirements above.

@practicalswift
Copy link
Contributor Author

Anyone willing to review? :-)

@practicalswift practicalswift force-pushed the init-and-validation-locks branch from db5fd51 to 8cf4342 Compare December 13, 2017 16:44
@practicalswift practicalswift force-pushed the init-and-validation-locks branch from 8cf4342 to 255b5c5 Compare December 21, 2017 12:38
Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just put a cs_main at the top of the load-from disk loop thing, there is no reason to lock and unlock cs_main 20 times during single-threaded init.

@practicalswift practicalswift force-pushed the init-and-validation-locks branch 2 times, most recently from 9d78b3c to c8164c8 Compare January 11, 2018 21:13
@practicalswift
Copy link
Contributor Author

@ajtowns @TheBlueMatt Thanks for reviewing! I've now addressed your feedback. Would you mind re-reviewing? :-)

@maflcko
Copy link
Member

maflcko commented Jan 12, 2018

General note: Instead of documenting the locking assumptions in the OP of this pull request, wouldn't it be better to put them in the header files? Imo, this increases review efficiency.

@practicalswift
Copy link
Contributor Author

@MarcoFalke You mean as an interim before we've added GUARDED_BY(…) annotations for each guarded variable?

@maflcko
Copy link
Member

maflcko commented Mar 12, 2018

No, I meant adding the GUARDED_BY to header files whenever possible

@practicalswift practicalswift force-pushed the init-and-validation-locks branch 2 times, most recently from 4502ab7 to 6bb006b Compare March 14, 2018 15:46
@practicalswift practicalswift changed the title Add missing locks to init.cpp (in AppInitMain + ThreadImport) and validation.cpp Add missing locks: validation.cpp + related Mar 14, 2018
@practicalswift practicalswift force-pushed the init-and-validation-locks branch 2 times, most recently from 96c1b9f to 27a0eaa Compare March 14, 2018 17:02
@practicalswift
Copy link
Contributor Author

@MarcoFalke Sure! New version with GUARDED_BY + the implied EXCLUSIVE_LOCKS_REQUIRED :-)

Could you please review? :-)

@practicalswift practicalswift force-pushed the init-and-validation-locks branch 2 times, most recently from b4a008f to 313c325 Compare March 23, 2018 15:05
@practicalswift
Copy link
Contributor Author

Hrmm, the i686-w64-mingw32 build seems to timeout for some reason.

@ryanofsky
Copy link
Contributor

Hrmm, the i686-w64-mingw32 build seems to timeout for some reason.

Should be fixed by rebasing after #12772

@practicalswift practicalswift force-pushed the init-and-validation-locks branch from 313c325 to 9bd9d9f Compare March 27, 2018 11:07
@practicalswift
Copy link
Contributor Author

Rebased!

@ajtowns
Copy link
Contributor

ajtowns commented Mar 28, 2018

9bd9d9f8302d4cdde2f25f4d7a008f8f0a0ccea2 looks good to me, fwiw, but it needs another rebase anyway to cope with scoped enums.

@practicalswift practicalswift force-pushed the init-and-validation-locks branch from 9bd9d9f to 6d9f3b6 Compare March 28, 2018 02:35
@practicalswift
Copy link
Contributor Author

Rebased!

@ajtowns Thanks for the review. Please re-review :-)

@TheBlueMatt
Copy link
Contributor

Please just put a cs_main at the top of the load-from disk loop thing, there is no reason to lock and unlock cs_main 20 times during single-threaded init.

Looks like you never did this.

@practicalswift practicalswift force-pushed the init-and-validation-locks branch 2 times, most recently from e7824b3 to 7e11716 Compare October 11, 2018 13:26
@practicalswift
Copy link
Contributor Author

practicalswift commented Oct 24, 2018

@TheBlueMatt Can you confirm that your suggestion is addressed in the latest version? :-) If not I might need help identifying the "load-from disk loop thing" :-)

@practicalswift
Copy link
Contributor Author

@TheBlueMatt Friendly ping :-)

@practicalswift
Copy link
Contributor Author

@MarcoFalke I'll do that once my other two open locking PRs #13128 and #13123 have been processed (either merged or closed). I try to limit my work in progress in the form of open pull requests :-)

@maflcko
Copy link
Member

maflcko commented Jan 10, 2019

I think this can be reopened and rebased.

@practicalswift
Copy link
Contributor Author

@MarcoFalke I force-pushed before re-opening, so now I'm unable to re-open. Could you re-open? :-)

@maflcko
Copy link
Member

maflcko commented Jan 10, 2019

I can't force push 7e11716932b8a1f6f1aa2c06e1990b64950a89bc to your branch either, since the pull request is closed.

Try

git push origin 7e11716932b8a1f6f1aa2c06e1990b64950a89bc:init-and-validation-locks -f
# reopen
git push origin init-and-validation-locks -f

@practicalswift practicalswift force-pushed the init-and-validation-locks branch from 7e11716 to f5b3548 Compare January 10, 2019 18:03
@practicalswift
Copy link
Contributor Author

practicalswift commented Jan 10, 2019

@MarcoFalke Thanks! Now re-opened and force-pushed. WIP for now.

@practicalswift
Copy link
Contributor Author

practicalswift commented Jan 10, 2019

@MarcoFalke I'm a bit unsure how to resolve the locking errors below (see Travis OS X output). Do you have any suggestion?

If m_wallet.chain().lock(…) succeeds then we have acquired cs_main and access to chainActive should be OK?

interfaces/wallet.cpp:335:22: error: reading variable 'chainActive' requires holding mutex 'cs_main' [-Werror,-Wthread-safety-analysis]
        num_blocks = ::chainActive.Height();
                     ^
interfaces/wallet.cpp:349:26: error: reading variable 'chainActive' requires holding mutex 'cs_main' [-Werror,-Wthread-safety-analysis]
            num_blocks = ::chainActive.Height();
                         ^
interfaces/wallet.cpp:380:22: error: reading variable 'chainActive' requires holding mutex 'cs_main' [-Werror,-Wthread-safety-analysis]
        num_blocks = ::chainActive.Height();
                     ^

@maflcko
Copy link
Member

maflcko commented Jan 10, 2019

The compiler doesn't understand that, though. You'd have to add a temporary annotation or use the interface (c.f. #14711)

@ryanofsky
Copy link
Contributor

ryanofsky commented Jan 10, 2019

re: #11652 (comment)

I'm a bit unsure how to resolve the locking errors below (see Travis OS X output). Do you have any suggestion?

You could use the LockAnnotation class to resolve these errors. See 081accb from #14437 for examples. Rebasing on top of #14711 would also resolve them.

@practicalswift
Copy link
Contributor Author

practicalswift commented Jan 10, 2019

@MarcoFalke Would you mind taking over this PR if it put it up for grabs? At the moment I'm afraid I don't have time to resolve the remaining issues.

@ryanofsky
Copy link
Contributor

At the moment I'm afraid I don't have time to resolve the remaining issues.

Are there other issues besides the need for lock annotations in the wallet? If not, we could just treat this PR as blocked until #14711 is merged, since it removes the need for them.

@maflcko maflcko force-pushed the init-and-validation-locks branch from f5b3548 to a04dd77 Compare January 15, 2019 21:12
@practicalswift
Copy link
Contributor Author

Closing in favour of PR #15191 and #15192. They cover everything in this PR except the chainActive locks.

I'll revisit the missing cs_main locks required for chainActive once #14711 is merged.

@practicalswift practicalswift deleted the init-and-validation-locks branch April 10, 2021 19:37
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants