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

addrmgr: make KnownAddress methods thread-safe #1763

Merged
merged 1 commit into from Oct 26, 2021

Conversation

chappjc
Copy link
Contributor

@chappjc chappjc commented Oct 20, 2021

This is an alternative resolution for the data races in #1760. I'm proposing both solutions in case one is more palatable to the maintainers than the other. Note that the approach in this PR was taken by dcrd's fix in decred/dcrd@ad33b0e

This gives KnownAddress a sync.RWMutex so the exported methods may safely access the na (*wire.NetAddress) and lastattempt fields.
The AddrManager is updated to lock the new KnownAddress mutex before assigning to na or lastattempt.
The other KnownAddress fields are only accessed by AddrManager, using its own mutex for synchronization.

This resolution requires no code changes in consumers like neutrino, just an update to the btcd module requirement. I've tested this with btcwallet --testnet --usespv -d debug, which uses neutrino and the affected code, using just a replace github.com/btcsuite/btcd => github.com/chappjc/btcd v0.22.0-beta.0.20211020165918-67e82a6dbe7f.

This gives KnownAddress a sync.RWMutex so the exported methods may
safely access the na (*wire.NetAddress) and lastattempt fields.
The AddrManager is updated to lock the new KnownAddress mutex before
assigning to na or lastattempt.
The other KnownAddress fields are only accessed by AddrManager, using
its own Mutex for synchronization.
@jcvernaleo
Copy link
Member

Thanks! I'll try to review this tomorrow but without looking at any of the code, my guess is that it makes the most sense to follow what decred does when we can (the fork becomes the upstream :) )

@coveralls
Copy link

Pull Request Test Coverage Report for Build 1364556215

  • 15 of 20 (75.0%) changed or added relevant lines in 2 files are covered.
  • 6 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.01%) to 52.98%

Changes Missing Coverage Covered Lines Changed/Added Lines %
addrmgr/knownaddress.go 4 6 66.67%
addrmgr/addrmanager.go 11 14 78.57%
Files with Coverage Reduction New Missed Lines %
addrmgr/addrmanager.go 1 73.51%
integration/rpctest/blockgen.go 2 83.67%
btcec/signature.go 3 92.82%
Totals Coverage Status
Change from base Build 1308902808: 0.01%
Covered Lines: 21100
Relevant Lines: 39826

💛 - Coveralls

@chappjc
Copy link
Contributor Author

chappjc commented Oct 20, 2021

Thanks! I'll try to review this tomorrow but without looking at any of the code, my guess is that it makes the most sense to follow what decred does when we can (the fork becomes the upstream :) )

Yep that makes sense. No point in diverging unnecessarily.

Copy link
Member

@jcvernaleo jcvernaleo left a comment

Choose a reason for hiding this comment

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

Looks all good to me.
OK

@jcvernaleo jcvernaleo merged commit a148fa7 into btcsuite:master Oct 26, 2021
@chappjc chappjc deleted the safe-ka-accessors branch October 26, 2021 16:13
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.

None yet

3 participants