add LOCK() for proxy related data-structures #1859

Merged
merged 1 commit into from Oct 7, 2012

Conversation

Projects
None yet
5 participants

Diapolo commented Sep 23, 2012

  • fix #1560 by properly locking proxy related data-structures
  • introduce GetProxyPair() and GetNameProxyPair() to be able to use a
    thread-safe local copy from proxyInfo and nameproxyInfo
  • rename GetNameProxy() into HaveNameProxy() to be more clear

@laanwj laanwj and 1 other commented on an outdated diff Sep 23, 2012

src/netbase.cpp
static proxyType nameproxyInfo;
+static CCriticalSection cs_nameproxyInfo;
@laanwj

laanwj Sep 23, 2012

Owner

Using one "proxy info" lock would be enough, no need for a separate one for the nameproxy.

@Diapolo

Diapolo Sep 23, 2012

Indeed, fixed!

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/bd5f5de6b8ab13ec34c96fa7376da329a07c1105 for binaries and test log.

Owner

laanwj commented Sep 24, 2012

ACK

@gavinandresen gavinandresen and 2 others commented on an outdated diff Sep 25, 2012

src/netbase.cpp
@@ -467,6 +474,7 @@ bool IsProxy(const CNetAddr &addr) {
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
{
+ LOCK(cs_proxyInfos);
@gavinandresen

gavinandresen Sep 25, 2012

Contributor

Holding the cs_proxyInfos lock all through ConnectSocket and it's subroutines is bad practice.

How about something like:
proxyType proxy; { LOCK(cs_proxyInfos); proxy = proxyInfo[addrDest.GetNetwork()]; }
... proceed, with local copy of proxy...

@laanwj

laanwj Sep 26, 2012

Owner

Or... We already have a GetProxy function to encapsulate getting values from proxyInfo, why not use it here?

@Diapolo

Diapolo Sep 26, 2012

Just to understand, a lock is persistent in a function scope, right? So when I have a lock in a sub-function used in ConnectSocket it is released, when returning back to ConnectSocket.

@laanwj

laanwj Sep 27, 2012

Owner

A scoped lock is acquired where you define it with LOCK(), and it is released immediately at the end of the block (}) in which you define it. So, yes.

@gavinandresen gavinandresen commented on an outdated diff Sep 25, 2012

src/netbase.cpp
@@ -504,6 +512,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
SplitHostPort(string(pszDest), port, strDest);
SOCKET hSocket = INVALID_SOCKET;
+ LOCK(cs_proxyInfos);
@gavinandresen

gavinandresen Sep 25, 2012

Contributor

Same comment here, maybe:
proxyType npInfo;
{
LOCK(); npInfo = nameproxyInfo;
}
... then proceed to use npInfo instead of the static nameproxyInfo.

Diapolo commented Sep 28, 2012

I'm going to update this until early next week :).

Diapolo commented Sep 28, 2012

Updated to include 2 new functions to use local copies of the proxyInfo and nameproxyInfo objects in the time-critical functions, which removes the bad practise of holding the lock all through ConnectSocket and it's subroutines, as Gavin suggested.

I chose to add these 2 function (currently not exposed to netbase.h), because I think I need those when updating the GUI with extended proxy settings.

@sipa sipa and 1 other commented on an outdated diff Sep 28, 2012

src/netbase.h
@@ -139,7 +139,7 @@ class CService : public CNetAddr
bool GetProxy(enum Network net, CService &addrProxy);
bool IsProxy(const CNetAddr &addr);
bool SetNameProxy(CService addrProxy, int nSocksVersion = 5);
-bool GetNameProxy();
+bool IsNameProxy();
@sipa

sipa Sep 28, 2012

Owner

What "is" a name proxy? I agree GetNameProxy() currently doesn't make sense either, but I'd go for HaveNameProxy().

@Diapolo

Diapolo Sep 28, 2012

Seems better suited, yes.

@laanwj laanwj commented on the diff Sep 28, 2012

src/netbase.cpp
nameproxyInfo = std::make_pair(addrProxy, nSocksVersion);
return true;
}
-bool GetNameProxy() {
+bool GetNameProxyPair(proxyType &nameproxyInfoOut) {
+ LOCK(cs_proxyInfos);
+ if (!nameproxyInfo.second)
+ return false;
+ nameproxyInfoOut = nameproxyInfo;
+ return true;
+}
+
+bool HaveNameProxy() {
+ LOCK(cs_proxyInfos);
return nameproxyInfo.second != 0;
}
bool IsProxy(const CNetAddr &addr) {
@laanwj

laanwj Sep 28, 2012

Owner

Edit: never mind, it does exactly what it says, I misunderstood the function.

@laanwj laanwj and 2 others commented on an outdated diff Sep 28, 2012

src/netbase.cpp
if (!proxyInfo[net].second)
return false;
addrProxy = proxyInfo[net].first;
return true;
}
+bool GetProxyPair(enum Network net, proxyType &proxyInfoOut) {
@laanwj

laanwj Sep 28, 2012

Owner

GetProxy already returns the same information (addrProxy, used), although in a slightly different form. Either keep this function or GetProxy, but we don't need both.

@Diapolo

Diapolo Sep 28, 2012

GetProxy() returns a CService, but I need the CService AND SOCKS version for the requested net, see ConnectSocket().
Perhaps you need to explain a little more :).

Edit: ConnectSocket() uses both values of the pair .first and .second.

@laanwj

laanwj Sep 28, 2012

Owner

Yeah, so replace GetProxy to return a proxyType and use that. Returning all the information is better IMO. I just don't see the need for two functions, one which returns a subset of the information that the other does.

@sipa

sipa Sep 28, 2012

Owner

Agree with @laanwj

@Diapolo

Diapolo Sep 28, 2012

I simply didn't understand what you wanted me to do ^^, now it's clear @laanwj.
I'll update tomorrow!

Diapolo commented Sep 29, 2012

@sipa I updated GetProxy() to fill a proxyType object and make that usable outsite of netbase.cpp (moved the typedef to netbase.h).
@laanwj Can you have a in depth look at the changes to optionsmodel.cpp, I included the use of GetProxy() for the SOCKS version, too, which was not in before (was read from the settings only).

@Diapolo Diapolo commented on the diff Sep 29, 2012

src/qt/optionsmodel.cpp
@@ -145,21 +145,26 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
case ProxyUse:
return settings.value("fUseProxy", false);
@Diapolo

Diapolo Sep 29, 2012

@laanwj I was asking myself, if this should be changed into, as data() returns active state for IP, port and SOCKS version, too.

        case ProxyUse: {
            proxyType proxy;
            return QVariant(GetProxy(NET_IPV4, proxy));
        }
@laanwj

laanwj Sep 29, 2012

Owner

Could be better because that better represents the current configuration, but leave that for a later pull. Aim to keep functionality precisely the same in these commits.

@Diapolo

Diapolo Sep 29, 2012

Understood :), thanks for your valuable input master!

@laanwj laanwj and 1 other commented on an outdated diff Sep 29, 2012

src/netbase.cpp
@@ -467,7 +481,9 @@ bool IsProxy(const CNetAddr &addr) {
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
{
- const proxyType &proxy = proxyInfo[addrDest.GetNetwork()];
+ proxyType proxy;
+ if (!GetProxy(addrDest.GetNetwork(), proxy))
+ return false;
@laanwj

laanwj Sep 29, 2012

Owner

This is new behavior -- I don't think you want to exit here with an error if there is no proxy

@Diapolo

Diapolo Sep 29, 2012

Indeed, that's a fault, same for the below occurance!
Should I memset the proxyType var to 0 or is this done by the compiler here?

Edit: Should be safe to leave it, because of default constructors used, right :)?

@laanwj

laanwj Sep 29, 2012

Owner

No need to zero it explicitly. It's an object with constructor (std::pair) and not a POD ("Plain Old Data") type. See http://stackoverflow.com/questions/9025792/does-the-default-constructor-of-stdpair-set-basic-types-int-etc-to-zero

@laanwj laanwj commented on an outdated diff Sep 29, 2012

src/netbase.cpp
@@ -504,19 +520,23 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
SplitHostPort(string(pszDest), port, strDest);
SOCKET hSocket = INVALID_SOCKET;
- CService addrResolved(CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port);
+
+ proxyType nameproxy;
+ if (!GetNameProxy(nameproxy))
@laanwj

laanwj Sep 29, 2012

Owner

Same here?

Owner

sipa commented Sep 30, 2012

ACK on changes to core.

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/fd31870d2820d7e1d0f8f3253f081aa71c0b2dca for binaries and test log.

Diapolo commented Sep 30, 2012

@laanwj Anything left for the Qt changes? I trie to not include feature-changes and will do that after this got merged.

Owner

laanwj commented Oct 2, 2012

ACK

Diapolo commented Oct 2, 2012

No code changes, only updated some code-layout in optionsmodel.cpp.

@laanwj laanwj and 1 other commented on an outdated diff Oct 4, 2012

src/netbase.cpp
@@ -467,7 +481,8 @@ bool IsProxy(const CNetAddr &addr) {
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
{
- const proxyType &proxy = proxyInfo[addrDest.GetNetwork()];
+ proxyType proxy;
+ GetProxy(addrDest.GetNetwork(), proxy);
@laanwj

laanwj Oct 4, 2012

Owner

I'd suggest using the return value of GetProxy here, instead of querying proxy.second later on. It's a little bit more readable.

@Diapolo

Diapolo Oct 4, 2012

Yes, good idea ... and updated :).

Philip Kaufmann add LOCK() for proxy related data-structures
- fix #1560 by properly locking proxy related data-structures
- update GetProxy() and introduce GetNameProxy() to be able to use a
  thread-safe local copy from proxyInfo and nameproxyInfo
- update usage of GetProxy() all over the source to match the new
  behaviour, as it now fills a full proxyType object
- rename GetNameProxy() into HaveNameProxy() to be more clear
81bbef2

Diapolo commented Oct 5, 2012

Can we get this fix into 0.7.1?

Owner

laanwj commented Oct 5, 2012

Yes as I said on the mailing list, that's the goal.

Avoiding corner-case random crashes is always good.

@sipa sipa added a commit that referenced this pull request Oct 7, 2012

@sipa sipa Merge pull request #1859 from Diapolo/proxy_locks
add LOCK() for proxy related data-structures
43de649

@sipa sipa merged commit 43de649 into bitcoin:master Oct 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment