Skip to content
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

Add locks when querying chainActive #485

Closed
wants to merge 9 commits into from

Conversation

jmjatlanta
Copy link

@jmjatlanta jmjatlanta commented Aug 27, 2021

Fixes Issue #482

The std::vector vChain within the CChain class is not thread safe. Various methods modify and query the vector with no synchronization. This fixes those found during testing.

TODO:

  • Check for deadlocks. Clang's static analysis tools may help.
  • Check for other AssertLockHeld failures (i.e. the wallet), and turn into an issue. (not part of this PR, just a reminder)

Only those that use chainActive
@jmjatlanta
Copy link
Author

jmjatlanta commented Sep 2, 2021

In the last few commits, I have updated the threadsafety annotations to work better with more recent versions of clang. With that, I have run the analysis and found the areas for cs_main that need locking.

See this file and search for warning:. There are 396 hits, some of which do not apply. The warning mutex 'm' is still held at... is a false positive.

For the majority of cases, we will need to determine if we should

  1. Already be locked further up the stack
  2. Lock for the duration of the call
  3. Lock for a portion of the call
  4. Lock only for the chainActive call (additional class methods could make this easier).

I am assuming for now we will continue to use exclusive locking. Small changes to the instrumentation will be necessary if we move to shared locking.

Trying to fix all these warnings as part of this PR is probably "scope creep". A separate effort is also needed to add instrumentation for other (non-cs_main) mutexes.

@jmjatlanta jmjatlanta marked this pull request as ready for review September 24, 2021 20:42
@ca333 ca333 requested review from DeckerSU and dimxy January 11, 2022 10:35
src/main.cpp Outdated Show resolved Hide resolved
src/komodo_bitcoind.cpp Outdated Show resolved Hide resolved
src/chain.h Outdated Show resolved Hide resolved
src/chain.h Show resolved Hide resolved
@dimxy
Copy link
Collaborator

dimxy commented Jan 14, 2022

Great that lock held asserts are added.
I can see there are still many places where functions like komodo_chainactive, komodo_chainactive_timestamp etc are called without locks so we need to continue work on improving synchronisation in the codebase (in next PRs)

dimxy
dimxy previously approved these changes Jan 17, 2022
Copy link
Collaborator

@dimxy dimxy left a comment

Choose a reason for hiding this comment

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

good to test the code for some time after merge into dev

@ca333 ca333 requested a review from dimxy February 20, 2022 18:03
@tonymorony
Copy link

these changes implemented in combined PR: #559

@tonymorony tonymorony closed this Sep 5, 2022
who-biz pushed a commit to who-biz/komodo that referenced this pull request Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants