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

Move read lock after hasHeadState #7427

Merged
merged 6 commits into from
Oct 3, 2020
Merged

Move read lock after hasHeadState #7427

merged 6 commits into from
Oct 3, 2020

Conversation

terencechain
Copy link
Member

This fixes a bug raised in #7423. hasHeadState already calls the read lock so dead lock could happen. This moved read lock to after hasHeadState

@terencechain terencechain requested a review from a team as a code owner October 3, 2020 16:08
@terencechain terencechain self-assigned this Oct 3, 2020
@terencechain terencechain changed the title Move read lock before hasHeadState Move read lock after hasHeadState Oct 3, 2020
@codecov
Copy link

codecov bot commented Oct 3, 2020

Codecov Report

Merging #7427 into master will decrease coverage by 0.05%.
The diff coverage is 42.85%.

@@            Coverage Diff             @@
##           master    #7427      +/-   ##
==========================================
- Coverage   60.12%   60.07%   -0.06%     
==========================================
  Files         419      419              
  Lines       30835    30231     -604     
==========================================
- Hits        18540    18161     -379     
+ Misses       9228     9082     -146     
+ Partials     3067     2988      -79     

Copy link
Contributor

@farazdagi farazdagi left a comment

Choose a reason for hiding this comment

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

Another way to solve it:

  • Define exported HasHeadState() which will lock the mutex (basically it will be what current non-exported hasHeadState() is).
  • Define non-exported hasHeadState() it will be lock free version, used internally in package, where lock is already obtained.

That way, everywhere where you know that you've obtained the lock (and author of the package normally knows its contents well), you use lock-free version (if lock is already acquired). And from outside package, you are guaranteed that access is controlled by lock.

That way you don't have to worry about order of statements (first check head, then obtain locks as in the current PR), as you will simply use lock-free version, if lock is already obtained.

The only downside of that method, is exposing HasHeadState() (not too much of an issue, but still).

Copy link
Contributor

@farazdagi farazdagi left a comment

Choose a reason for hiding this comment

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

  • In order to avoid duplication, can we use lock-free version of func in exported one (I've added corresponding suggestion)
  • Removed bunch of added blank lines (if you've added them intentionally, please, ignore suggested code changes)

beacon-chain/blockchain/head.go Outdated Show resolved Hide resolved
beacon-chain/blockchain/chain_info.go Outdated Show resolved Hide resolved
beacon-chain/blockchain/chain_info.go Outdated Show resolved Hide resolved
beacon-chain/blockchain/chain_info.go Outdated Show resolved Hide resolved
terencechain and others added 4 commits October 3, 2020 12:51
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
@prylabs-bulldozer prylabs-bulldozer bot merged commit 7b3efcf into master Oct 3, 2020
@delete-merged-branch delete-merged-branch bot deleted the fix-head-lock branch October 3, 2020 20:53
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.

2 participants