cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Multi interface infinite loop

From: Daniel Stenberg <daniel-curl_at_haxx.se>
Date: Thu, 22 Apr 2004 08:04:13 +0200 (CEST)

On Wed, 21 Apr 2004, codemastr wrote:

> Ok got a question, I'm using the multi interface. And here is the scenario.
> Say a connection "fails" (times out). The below snippet's comments
> illustrate my problem
>
> /* This finishes with cont != 0 so while is entered */
> while(CURLM_CALL_MULTI_PERFORM == curl_multi_perform(multihandle, &cont))
> ;
>
> while(cont) {
> struct timeval timeout;
> int rc;
>
> fd_set fdread, fdwrite, fdexcep;
> int maxfd;
> FD_ZERO(&fdread);
> FD_ZERO(&fdwrite);
> FD_ZERO(&fdexcep);
> timeout.tv_sec = 1;
> timeout.tv_usec = 0;
>
> curl_multi_fdset(multihandle, &fdread, &fdwrite, &fdexcep, &maxfd);
>
> rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
> /* rc = 0 (timeout) */
>
> switch(rc) {
> case -1:
> case 0:
> /* jumps back to the top of the while */
> break;
> default:
> while(CURLM_CALL_MULTI_PERFORM ==
> curl_multi_perform(multihandle, &cont))
> ;
> }

You should still call curl_multi_perform() at least every N second, even when
select() times out (where N shouldn't be very many seconds). Otherwise you
won't get libcurl's timeouts and retries etc to work properly. This is
especially important if libcurl uses ares, but since you most likely want your
app to be unaware of such implementation details it should always do it.

> Since cont was set to !0 that one time, the loop is infinite. I copied this
> code pretty much from the examples, so I'm wondering what is wrong.

We might need to update the examples.

> If a timeout or select error occurs, what should I do?

select() shouldn't return error at all, that's an error case.

> Basically, it seems curl is not recognizing that select() timed out, and is
> instead continuing to try and connect.

I can't recognize any timeout since it hasn't run!

-- 
     Daniel Stenberg -- http://curl.haxx.se -- http://daniel.haxx.se
      Dedicated custom curl help for hire: http://haxx.se/curl.html
Received on 2004-04-22