Test whether created sockets are select()able #6412

Merged
merged 1 commit into from Jul 20, 2015

Conversation

Projects
None yet
4 participants
@sipa
Member

sipa commented Jul 9, 2015

This provides a belt-and-suspends check against file descriptor overflowing, to fix #6411.

@luke-jr

View changes

src/net.cpp
@@ -385,6 +385,12 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, Params().GetDefaultPort(), nConnectTimeout, &proxyConnectionFailed) :
ConnectSocket(addrConnect, hSocket, nConnectTimeout, &proxyConnectionFailed))
{
+ if (!IsSelectableSocket(hSocket)) {
+ LogPrintf("Cannot create connection: non-selectable socket created (file descriptor limit exceeded?)\n");

This comment has been minimized.

@luke-jr

luke-jr Jul 9, 2015

Member

Suggest removing " (file descriptor limit exceeded?)" since this description is inaccurate and likely to confuse someone trying to debug it.

@luke-jr

luke-jr Jul 9, 2015

Member

Suggest removing " (file descriptor limit exceeded?)" since this description is inaccurate and likely to confuse someone trying to debug it.

This comment has been minimized.

@sipa

sipa Jul 9, 2015

Member

"non-selectable socket created" is completely unhelpful for indicating the reason or helping debug it. So I prefer to explain it a bit at least. Do you have a better suggestion?

@sipa

sipa Jul 9, 2015

Member

"non-selectable socket created" is completely unhelpful for indicating the reason or helping debug it. So I prefer to explain it a bit at least. Do you have a better suggestion?

This comment has been minimized.

@laanwj

laanwj Jul 10, 2015

Member

I'd say the file descriptor limit exceeded is the part of the message that matters most.

@laanwj

laanwj Jul 10, 2015

Member

I'd say the file descriptor limit exceeded is the part of the message that matters most.

This comment has been minimized.

@luke-jr

luke-jr Jul 10, 2015

Member

" (socket cannot be included in fdset)" perhaps? The actual fd limit very much has not been exceeded in most situations you'd get this error...

@luke-jr

luke-jr Jul 10, 2015

Member

" (socket cannot be included in fdset)" perhaps? The actual fd limit very much has not been exceeded in most situations you'd get this error...

This comment has been minimized.

@laanwj

laanwj Jul 10, 2015

Member

Well OK @luke-jr is right in that this is not "the" file descriptor limit, just the select file descriptor limit. Let's just name the beast as it is:
Cannot create connection: non-selectable socket created (>=FD_SETSIZE)

@laanwj

laanwj Jul 10, 2015

Member

Well OK @luke-jr is right in that this is not "the" file descriptor limit, just the select file descriptor limit. Let's just name the beast as it is:
Cannot create connection: non-selectable socket created (>=FD_SETSIZE)

@ajweiss

This comment has been minimized.

Show comment
Hide comment
@ajweiss

ajweiss Jul 9, 2015

Contributor

select() is also used in netbase, seems it would make sense to check there. Also, if connect() were to return an EINVAL for any reason, that could trigger this in netbase. Have you considered just printing the fd value? For language I'd go simple "invalid file descriptor %ud from netbase on connect".

Contributor

ajweiss commented Jul 9, 2015

select() is also used in netbase, seems it would make sense to check there. Also, if connect() were to return an EINVAL for any reason, that could trigger this in netbase. Have you considered just printing the fd value? For language I'd go simple "invalid file descriptor %ud from netbase on connect".

@laanwj

This comment has been minimized.

Show comment
Hide comment
@laanwj

laanwj Jul 10, 2015

Member

utACK

Member

laanwj commented Jul 10, 2015

utACK

@@ -92,4 +92,12 @@ typedef u_int SOCKET;
size_t strnlen( const char *start, size_t max_len);
#endif // HAVE_DECL_STRNLEN
+bool static inline IsSelectableSocket(SOCKET s) {
+#ifdef WIN32
+ return true;

This comment has been minimized.

@luke-jr

luke-jr Jul 10, 2015

Member

return s != INVALID_SOCKET; ?

@luke-jr

luke-jr Jul 10, 2015

Member

return s != INVALID_SOCKET; ?

This comment has been minimized.

@laanwj

laanwj Jul 10, 2015

Member

Don' think it should double as "is this an error return value"

@laanwj

laanwj Jul 10, 2015

Member

Don' think it should double as "is this an error return value"

@sipa

This comment has been minimized.

Show comment
Hide comment
@sipa

sipa Jul 10, 2015

Member

Updated. The test is now done also in netbase (causing simple failure, as netbase does not usually print error messages), and when creating a listen socket.

Member

sipa commented Jul 10, 2015

Updated. The test is now done also in netbase (causing simple failure, as netbase does not usually print error messages), and when creating a listen socket.

@laanwj

This comment has been minimized.

Show comment
Hide comment
@laanwj

laanwj Jul 10, 2015

Member

Needs backport to 0.11 and 0.10 (doesn't seem to involved, I'm willing to do that)

Member

laanwj commented Jul 10, 2015

Needs backport to 0.11 and 0.10 (doesn't seem to involved, I'm willing to do that)

@sipa

This comment has been minimized.

Show comment
Hide comment
@sipa

sipa Jul 10, 2015

Member

See my branches nonselectsocket-0.11 and nonselectsocket-0.10.

Member

sipa commented Jul 10, 2015

See my branches nonselectsocket-0.11 and nonselectsocket-0.10.

@laanwj

View changes

src/net.cpp
@@ -1597,6 +1608,13 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste
LogPrintf("%s\n", strError);
return false;
}
+ if (!IsSelectableSocket(hListenSocket))
+ {
+ strError = strprintf("Error: Couldn't create a listenable socket for incoming connections");

This comment has been minimized.

@laanwj

laanwj Jul 10, 2015

Member

strprintf doesn't work without arguments (and is redundant)

@laanwj

laanwj Jul 10, 2015

Member

strprintf doesn't work without arguments (and is redundant)

@sipa

This comment has been minimized.

Show comment
Hide comment
@sipa

sipa Jul 10, 2015

Member

@laanwj Done.

Member

sipa commented Jul 10, 2015

@laanwj Done.

@laanwj

This comment has been minimized.

Show comment
Hide comment
@laanwj

laanwj Jul 13, 2015

Member

This brings up a warning, as SOCKET is defined as unsigned int:

compat.h: In function ‘bool IsSelectableSocket(SOCKET)’:
compat.h:99:18: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
     return (s >= 0 && s < FD_SETSIZE);
Member

laanwj commented Jul 13, 2015

This brings up a warning, as SOCKET is defined as unsigned int:

compat.h: In function ‘bool IsSelectableSocket(SOCKET)’:
compat.h:99:18: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
     return (s >= 0 && s < FD_SETSIZE);
@laanwj

This comment has been minimized.

Show comment
Hide comment
@laanwj

laanwj Jul 17, 2015

Member

@sipa are you planning to fix the warning or should we not hold this up on that?

Member

laanwj commented Jul 17, 2015

@sipa are you planning to fix the warning or should we not hold this up on that?

@laanwj laanwj merged commit d422f9b into bitcoin:master Jul 20, 2015

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

laanwj added a commit that referenced this pull request Jul 20, 2015

Merge pull request #6412
d422f9b Test whether created sockets are select()able (Pieter Wuille)

laanwj added a commit that referenced this pull request Jul 20, 2015

Fix warning introduced by #6412
SOCKET are defined as unsigned integers, thus always >=0.

laanwj added a commit that referenced this pull request Jul 20, 2015

Fix warning introduced by #6412
SOCKET are defined as unsigned integers, thus always >=0.

Rebased-From: 89289d8
@laanwj

This comment has been minimized.

Show comment
Hide comment
@laanwj

laanwj Jul 20, 2015

Member

Backported to 0.11 as e8b87c8
Warning fixed in 89289d8 (e8b87c8 in 0.11)

To 0.10 as 0739e6e ae52a7f

Member

laanwj commented Jul 20, 2015

Backported to 0.11 as e8b87c8
Warning fixed in 89289d8 (e8b87c8 in 0.11)

To 0.10 as 0739e6e ae52a7f

sipa added a commit that referenced this pull request Jul 20, 2015

Test whether created sockets are select()able
Conflicts:
	src/net.cpp

Github-Pull: #6412
Rebased-From: d422f9b

laanwj added a commit that referenced this pull request Jul 20, 2015

Fix warning introduced by #6412
SOCKET are defined as unsigned integers, thus always >=0.

Rebased-From: 89289d8

dexX7 added a commit to OmniLayer/omnicore that referenced this pull request Aug 18, 2015

Merge pull request #181
ae52a7f Fix warning introduced by #6412 (Wladimir J. van der Laan)
0739e6e Test whether created sockets are select()able (Pieter Wuille)
255eced Updated URL location of netinstall for Debian (฿tcDrak)
7e66e9c openssl: avoid config file load/race (Cory Fields)
3f55638 doc: update mailing list address (Wladimir J. van der Laan)
be64204 Add option `-alerts` to opt out of alert system (Wladimir J. van der Laan)
0fd8464 Fix getbalance * (Tom Harding)
09334e0 configure: Detect (and reject) LibreSSL (Luke Dashjr)
181771b json: fail read_string if string contains trailing garbage (Wladimir J. van der Laan)
ecc96f5 Remove P2SH coinbase flag, no longer interesting (Luke Dashjr)
ebd7d8d Parameter interaction: disable upnp if -proxy set (Wladimir J. van der Laan)
ae3d8f3 Fix two problems in CSubNet parsing (Wladimir J. van der Laan)
e4a7d51 Simplify code for CSubnet (Wladimir J. van der Laan)

Conflicts:
	README.md

@dgenr8 dgenr8 referenced this pull request in bitcoinxt/bitcoinxt May 18, 2017

Merged

Networking improvements #203

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