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 safe accessors for addresses #1760

Closed
wants to merge 1 commit into from

Commits on Oct 14, 2021

  1. addrmgr: make safe accessors for addresses

    The KnownAddress returned by GetAddress is not safely usable while the
    AddrManager is in use by another goroutine since the fields of
    the KnownAddress are mutable. Although AddrManager treats a
    *wire.NetAddress as immutable, the KnownAddress.na field itself
    is reassigned. Similarly, the lastattempt field is mutable.
    
    To address this API usability issue, this commit makes two new accessor
    methods:
    
    - GetNetAddress returns the *wire.NetAddress and the lastAttempt
    - GetAddressCopy returns a copy of the *KnownAddress
    
    The original GetAddress remains because it is designed to reference
    the KnownAddress that is still in the AddrManager, allowing operations
    by the AddrManager such as connects and attempts to be reflected in the
    initially returned KnownAddress. The blackbox addrmgr tests are
    are designed around this behavior.
    
    However, it is not safe to use GetAddress from another goroutine such
    as the connmgr's GetNewAddress function (newAddressFunc closure defined
    in newServer) concurrent with updates to the KnownAddress such as via
    (*Peer).inHandler, which has callbacks to the AddrManager that may
    mutate the KnownAddress.
    
    An alternate resolution would involve adding a Mutex to a KnownAddress,
    but this would be a more substantial change, which would likely create
    unnecessary lock contention and complexity. Returning copies is safer
    and simpler.
    chappjc committed Oct 14, 2021
    Configuration menu
    Copy the full SHA
    25f12e8 View commit details
    Browse the repository at this point in the history