2

Most of the time this code works just fine. But sometimes when the executable has been running for a while, select() appears to time out immediately, then get into a weird state where it keeps getting called, timing out immediately, over and over. Then it has to be killed from the outside.

My guess would be that the way that standard input changes overtime is at fault - that is what select is blocking on.

Looking around on StackOverflow, most of people's select() troubles seem to be solved by making sure to reset with the macros (FD_ZERO & FD_SET) every time and using the right initial parameter to select. I don't think those are the issues here.

int            rc     = 0;
fd_set         fdset;
struct timeval timeout;

// -- clear out the response -- //
readValue = "";

// -- set the timeout -- //
timeout.tv_sec = passedInTimeout;  // 5 seconds
timeout.tv_usec = 0;

// -- indicate which file descriptors to select from -- //
FD_ZERO(&fdset);
FD_SET(passedInFileDescriptor, &fdset); //passedInFileDescriptor = 0

// -- perform the selection operation, with timeout -- //
rc = select(1, &fdset, NULL, NULL, &timeout);


if (rc == -1)  // -- select failed -- //
{
    result = TR_ERROR;
}
else if (rc == 0)  // -- select timed out -- //
{
    result = TR_TIMEDOUT;
}
else 
{
    if (FD_ISSET(mFileDescriptor, &fdset))
    {
        if(rc = readData(readValue) <= 0)
        {
            result = TR_ERROR;
        }
    } else {
       result = TR_SUCCESS;
    }
}
3
  • 1
    Can you add some debugging code in the rc == 0 case? Log the value of passedInFileDescriptor, the fields in timeout, and FD_ISSET(mFileDescriptor, &fdset); something fishy is going on if this summarized code is correct, and you might need a lot more data to try to pin it down.
    – sarnold
    Apr 1, 2011 at 19:47
  • Calls ioctl() and tcsetattr() change the content of the file descriptor. A common file descriptor for an open file can be shared between parent and child, and it is possible for the child altering the descriptor which then propagates to the parent. This assumes you are not making ioctl() or similar calls yourself. I am thinking of non-blocking behavior of a file. Apr 1, 2011 at 19:48
  • Has the file descriptor been closed or reached eof? Apr 1, 2011 at 21:27

4 Answers 4

1

Beware that some implementaions of "select" apply strictly the specification: "nfds is the highest-numbered file descriptor in any of the three sets, plus 1". So, you'd better to change "1" with "passedInFileDescriptor+1" as first parameter. I don't know if this can solve your problem, but at least your code becomes more... uhm... "traditional" ;)

Bye

2
  • Thanks! I modified it. No changes to the behavior but I'm glad to be more correct.
    – ag6
    Apr 1, 2011 at 21:44
  • Hi! Is "mFileDescriptor" always equal to "passedInFileDescriptor"? If it's not true, something weird may happen. E.g: what does it happen if "mFileDescriptor" is wrong AND "FD_ISSET" succeed AND "readData() > 0"? "result" is undefined in your code, so... uhm... Ok, mine is just a hint. Bye! Apr 2, 2011 at 9:17
1

On some OSes, timeout is modified when calling select to reflect the amount of time not slept. It doesn't look like you're reusing timeout in your example, but make sure that you are indeed reinitializing it to 5 seconds every time before calling select.

1

I'm having the same problem, it works fine on windows but not on linux and I have the maxfd set to last socket + 1. It occurs periodically after long runs. I pick up the connection on accept and then the first call to select periodically times out.

0

Look at this code:

if (FD_ISSET(mFileDescriptor, &fdset))
{
    if(rc = readData(readValue) <= 0)
    {
        result = TR_ERROR;
    }
} else { 
   result = TR_SUCCESS;
}

There are two things bothering me here:

  1. if your FD has no data in it (like, say, an error occured), FD_ISSET() will return false and your function returns TR_SUCCESS !?
  2. you FD_SET(passedInFileDescriptor, &fdset), but check on another value: FD_ISSET(mFileDescriptor, &fdset). If mFileDescriptor != passedInFileDescriptor at some point, you'll fall into my first assumption.

It should be looking like this:

if (FD_ISSET(passedInFileDescriptor, &fdset))
{
    if(rc = readData(readValue) <= 0)
    {
        result = TR_ERROR;
    }
    else 
    {
        result = TR_SUCCESS;
    }
}
else
{
    result = TR_ERROR;
}

No?

(Edit: also, this answer also points the problem of your use of select() with a bad high_fd value)

Another edit: well, looks like the guys never came back... frustrating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.