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

select()'s first argument should be zero, if no file descriptors are selected #1786

Merged
merged 1 commit into from Sep 7, 2012

Conversation

jgarzik
Copy link
Contributor

@jgarzik jgarzik commented Sep 5, 2012

The most portable invocation for the zero-fds case, a widely used
portable hack for sleeping, generally involves passing NULL arguments
when the fd sets are unused. This form is used here.

Intended to supercede #1772.

@laanwj
Copy link
Member

laanwj commented Sep 5, 2012

Yes, adding an extra flag should solve the problem too. ACK

@Diapolo
Copy link

Diapolo commented Sep 5, 2012

This does not fix 10022 spam on Windows, as WSAEINVAL (10022) is caused by: The time-out value is not valid, or all three descriptor parameters were null..

But I think it is another fix worth to merge in addition to #1772, so ACK.

int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
if (have_fds)
nSelect = select(hSocketMax + 1,
&fdsetRecv, &fdsetSend, &fdsetError, &timeout);
Copy link

Choose a reason for hiding this comment

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

What adds such a line-break to readability?

@jgarzik
Copy link
Contributor Author

jgarzik commented Sep 5, 2012

@Diapolo so you actually tested this on Windows, for the net and !net cases? I'm not sure the Windows expected behavior matches what you describe...

@laanwj
Copy link
Member

laanwj commented Sep 5, 2012

Indeed, that WSAEINVAL: the time-out value is not valid, or all three descriptor parameters were null comes from the MS documentation http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx

So yes on Windows you still need the check to prevent error spam. It appears that windows does not allow for using select as simply a timeout. However, now you can check for have_fds instead of hSocketMax!=0, which is at least correct.

@jgarzik
Copy link
Contributor Author

jgarzik commented Sep 5, 2012

Yes, I understand it comes from the documentation, but is it actually seen after applying this patch?

@Diapolo
Copy link

Diapolo commented Sep 5, 2012

The error 10022 is still logged with this and without my patch, as select() returns WSAEINVAL. I tried it :).

@laanwj Yes, a check for have_fds is working, I can update my pull after this one is in.

@laanwj
Copy link
Member

laanwj commented Sep 5, 2012

It makes sense that it still occurs because windows ignores the nfds argument. So this change is a no-op from the viewpoint of winsock.

We could spare ourselves worrying about all these kinds of low-level OS details if we used an abstraction such as boost::asio (which will also use more efficient mechanisms such as epoll on platforms that support them).

@Diapolo
Copy link

Diapolo commented Sep 5, 2012

That is what I tried to say @jgarzik, his patch does not prevent the 10022 spam, I read the select() documentation on MSDN more than once to understand what is happening ^^.

I rather sure most part of that network code are still from Satoshi (at least he was the one who merged them to Github at first).

@jgarzik
Copy link
Contributor Author

jgarzik commented Sep 5, 2012

  1. Updated to always pass fd sets to select(), even when zero fd's are set.
  2. Use precise have_fds test before squawking on socket error.

{
int nErr = WSAGetLastError();
Copy link

Choose a reason for hiding this comment

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

When making my pulls obsolete, which makes sense here to have 1 instead of 2 to fix this, could you please merge the WSAGetLastError() into the printf :)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is nicer this way, especially if you are stepping through with a debugger.

@Diapolo
Copy link

Diapolo commented Sep 5, 2012

Verified to work and fix the 10022 spam on Windows, tried it and closed #1772 in favor of your patch.

@laanwj
Copy link
Member

laanwj commented Sep 6, 2012

ACK

laanwj added a commit that referenced this pull request Sep 7, 2012
select()'s first argument should be zero, if no file descriptors are selected
@laanwj laanwj merged commit f106491 into bitcoin:master Sep 7, 2012
@BitcoinPullTester
Copy link

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

@jgarzik jgarzik deleted the select-fix branch August 24, 2014 04:19
KolbyML pushed a commit to KolbyML/bitcoin that referenced this pull request Dec 5, 2020
e04d2bd Cleanup: removing unused mapRelay and cs_mapRelay. (furszy)

Pull request description:

  Cleaning the currently not unused `mapRelay` and `cs_mapRelay`. Making bitcoin#1786 obsolete.

ACKs for top commit:
  random-zebra:
    utACK e04d2bd
  Fuzzbawls:
    utACK e04d2bd

Tree-SHA512: ccbbec9f5e04995982e891191c844c3f81d4c06798d35f80ab52c0e07c05dd5ab15bf3a8c3e2476ae282794c00cb533ba807116142db0d455cf3791424ec5ab8
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants