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

addrman: Add missing lock in Clear() (CAddrMan) #11585

Merged

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Oct 31, 2017

Add missing lock in Clear() (CAddrMan).

The variable vRandom is guarded by the mutex cs.

Note to reviewers: Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only std::vector<int>().swap(vRandom);?

The variable vRandom is guarded by the mutex cs.
@fanquake fanquake added the P2P label Oct 31, 2017
@promag
Copy link
Member

promag commented Oct 31, 2017

ACK 3ab545d.

I think all CAddrMan members should be guarded by cs, so LGTM as it is:

bitcoin/src/addrman.h

Lines 184 to 186 in 8335cb4

private:
//! critical section to protect the inner data structures
mutable CCriticalSection cs;

@practicalswift
Copy link
Contributor Author

practicalswift commented Oct 31, 2017

Thanks for the quick review @promag!

@promag, you mean that all of the following should be guarded by cs? If so I think we might be missing some additional locks. Let me know and I'll investigate.

    //! last used nId
    int nIdCount;

    //! table with information about all nIds
    std::map<int, CAddrInfo> mapInfo;

    //! find an nId based on its network address
    std::map<CNetAddr, int> mapAddr;

    //! randomly-ordered vector of all nIds
    std::vector<int> vRandom;

    // number of "tried" entries
    int nTried;

    //! list of "tried" buckets
    int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];

    //! number of (unique) "new" entries
    int nNew;

    //! list of "new" buckets
    int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];

    //! last time Good was called (memory only)
    int64_t nLastGood;

protected:
    //! secret key to randomize bucket select with
    uint256 nKey;

    //! Source of random numbers for randomization in inner loops
    FastRandomContext insecure_rand;

@promag
Copy link
Member

promag commented Oct 31, 2017

That is what

//! critical section to protect the inner data structures
says. It seems public methods do lock cs.

@practicalswift
Copy link
Contributor Author

@promag I'll investigate! :-)

@practicalswift
Copy link
Contributor Author

practicalswift commented Oct 31, 2017

@promag You're right! The locking seems to be correct for the other CAddrMan members. Thanks!

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

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

utACK 3ab545d. CAddrMan::Clear is only called during initialization (during construction and deserialization) so it seems safe to add this lock. It would be good if the critical section comment was clearer about whether a lock is needed to access all class members or just some:

//! critical section to protect the inner data structures

@theuni probably knows more

@theuni
Copy link
Member

theuni commented Nov 1, 2017

utACK 3ab545d. I believe the intention is for all public methods to lock the cs, and the privates ones don't need to. That would imply that all vars should be covered under the cs.

@TheBlueMatt
Copy link
Contributor

utACK 3ab545d

@maflcko maflcko merged commit 3ab545d into bitcoin:master Nov 7, 2017
maflcko pushed a commit that referenced this pull request Nov 7, 2017
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Dec 22, 2019
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 2, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 4, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 12, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jan 16, 2020
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
DeckerSU added a commit to DeckerSU/KomodoOcean that referenced this pull request Jun 7, 2020
DeckerSU added a commit to DeckerSU/komodo that referenced this pull request Jun 7, 2020
TheComputerGenie added a commit to PirateNetwork/PirateOcean that referenced this pull request Oct 30, 2020
* New user params

* no splash fix

* no splash fix

* Update Ocean_all_cd.yml

until openssl and python2 breaking changes fixes are finalized
actions/runner-images#1811 (comment)

* + CountBits in crypto/common.h 

Return the smallest number n such that (x >> n) == 0 (or 64 if the highest bit in x is set
DeckerSU/KomodoOcean@d836179

* Add asmap utility which queries a mapping

The scripts for creating a compact IP->ASN mapping are here:
https://github.com/sipa/asmap

bitcoin/bitcoin@8feb4e4
DeckerSU/KomodoOcean@50daf84

* p2p: supplying and using asmap to improve IP bucketing in addrman

first approach to porting bitcoin/bitcoin#16702 into old v0.13 codebase.
DeckerSU/KomodoOcean@bfaf287

* Return mapped AS in RPC call getpeerinfo and in GUI 

DeckerSU/KomodoOcean@81d10f5

* Addrman Google Tests

Note: these are standart tests, which is not include asmap features.

DeckerSU/KomodoOcean@2173036

* add legacy and asmap addrman tests 

DeckerSU/KomodoOcean@7567965

* when clearing addrman clear mapInfo and mapAddr 

bitcoin/bitcoin@b86a420
DeckerSU/KomodoOcean@9b2c1f3

* add addrman serialization test 

DeckerSU/KomodoOcean@18ebbd5

* addrman: Add missing lock in Clear() (CAddrMan) 

bitcoin/bitcoin#11585
DeckerSU/KomodoOcean@b81d07b

* gui: debug window -> tab peers elems re-arrange 

added scroll area for detailed peer info, added splitter
between detailed info and peer table
DeckerSU/KomodoOcean@be106ed

* depends: tar: Always extract as yourself 

For normal users, --no-same-owner is default, but not so for root, where
it is assumed that root can change ownership willy-nilly. This is not
the case for privilege-limited container environments where we gaslight
the process into thinking it's root.

KomodoPlatform/komodo#368
bitcoin/bitcoin@89bee1b
DeckerSU/KomodoOcean@85d2fa6

* refactor: remove unused header var in GetBlockProof 

DeckerSU/KomodoOcean@a1be01c

* bump versions

CLIENT_VERSION 4.0.2
KOMODO_VERSION 0.6.1

* add display service bits for NSPV, ADDRINDEX and SPENTINDEX 

DeckerSU/KomodoOcean@f0ef3e2

* optional disable nspv 

nSPV processing is disabled by default for Komodo-Qt, to enable it
you can alsways use `-nspv_msg` command line arg.

KomodoPlatform/komodo#382
DeckerSU/KomodoOcean@1d1e568

* update getblocktemplate conditions 

DeckerSU/KomodoOcean@506bc22

* update IsNotInSync() conditions 

IsNotInSync() using now only in waitForPeers() which is called
in VerusStaker(...) and BitcoinMiner_noeq(...), mean which used
only in verushash-algo based mining and staking chains.
DeckerSU/KomodoOcean@cb5dec1

* fetch bdb using https instead of http 

DeckerSU/KomodoOcean@d09c03a

* remove auto-created file (cryptoconditions/compile) 

to resolve issues with compiling dirty releases from unchanged repo.
DeckerSU/KomodoOcean@3e4aa66
TheComputerGenie added a commit to PirateNetwork/pirate that referenced this pull request Oct 31, 2020
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Dec 5, 2020
87e33eb addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Straightforward cherry-pick from bitcoin#11585

ACKs for top commit:
  furszy:
    utACK 87e33eb
  Fuzzbawls:
    utACK 87e33eb

Tree-SHA512: a35ae05c141d0b29d834045aa7496a72469fc9d439175c1706c4f20283a6e51d234e2f0ee38d049a16fcad7cd882c5d55ef54b93e2ba47f99c580315b0eacbaf
@practicalswift practicalswift deleted the missing-lock-in-addrman-clear branch April 10, 2021 19:33
LarryRuane pushed a commit to LarryRuane/zcash that referenced this pull request Apr 13, 2021
The variable vRandom is guarded by the mutex cs.

zcash: cherry picked from commit 3ab545d
zcash: bitcoin/bitcoin#11585
zkbot added a commit to zcash/zcash that referenced this pull request Apr 17, 2021
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.
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Jul 1, 2021
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Mar 25, 2022
3ab545d addrman: Add missing lock in Clear() (CAddrMan) (practicalswift)

Pull request description:

  Add missing lock in `Clear()` (`CAddrMan`).

  The variable `vRandom` is guarded by the mutex `cs`.

  **Note to reviewers:** Does this look correct? Should the lock cover the entire scope of the method, or should it be limited to cover only `std::vector<int>().swap(vRandom);`?

Tree-SHA512: 8833f31beaed1728fa55b13ddf9e0b8e24e395931497329be2440ce1c5113ff02871707d40830260adabd30c4ea86088f5da5cf8a821150c0d820f50a2ce386a
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants