Unverified Commit 34fe0e16 authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

curl_easy_perform: fix timeout handling

curl_multi_wait() was erroneously used from within
curl_easy_perform(). It could lead to it believing there was no socket
to wait for and then instead sleep for a while instead of monitoring the
socket and then miss acting on that activity as swiftly as it should
(causing an up to 1000 ms delay).

Reported-by: Antoni Villalonga
Fixes #3305
Closes #3306
Closes #3308
parent ae7a09db
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -664,12 +664,12 @@ static CURLcode easy_transfer(struct Curl_multi *multi)


  while(!done && !mcode) {
  while(!done && !mcode) {
    int still_running = 0;
    int still_running = 0;
    int rc;
    bool gotsocket = FALSE;


    mcode = curl_multi_wait(multi, NULL, 0, 1000, &rc);
    mcode = Curl_multi_wait(multi, NULL, 0, 1000, NULL, &gotsocket);


    if(!mcode) {
    if(!mcode) {
      if(!rc) {
      if(!gotsocket) {
        long sleep_ms;
        long sleep_ms;


        /* If it returns without any filedescriptor instantly, we need to
        /* If it returns without any filedescriptor instantly, we need to
@@ -688,6 +688,7 @@ static CURLcode easy_transfer(struct Curl_multi *multi)


    /* only read 'still_running' if curl_multi_perform() return OK */
    /* only read 'still_running' if curl_multi_perform() return OK */
    if(!mcode && !still_running) {
    if(!mcode && !still_running) {
      int rc;
      CURLMsg *msg = curl_multi_info_read(multi, &rc);
      CURLMsg *msg = curl_multi_info_read(multi, &rc);
      if(msg) {
      if(msg) {
        result = msg->data.result;
        result = msg->data.result;
+18 −2
Original line number Original line Diff line number Diff line
@@ -985,11 +985,12 @@ CURLMcode curl_multi_fdset(struct Curl_multi *multi,


#define NUM_POLLS_ON_STACK 10
#define NUM_POLLS_ON_STACK 10


CURLMcode curl_multi_wait(struct Curl_multi *multi,
CURLMcode Curl_multi_wait(struct Curl_multi *multi,
                          struct curl_waitfd extra_fds[],
                          struct curl_waitfd extra_fds[],
                          unsigned int extra_nfds,
                          unsigned int extra_nfds,
                          int timeout_ms,
                          int timeout_ms,
                          int *ret)
                          int *ret,
                          bool *gotsocket) /* if any socket was checked */
{
{
  struct Curl_easy *data;
  struct Curl_easy *data;
  curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
@@ -1003,6 +1004,9 @@ CURLMcode curl_multi_wait(struct Curl_multi *multi,
  int retcode = 0;
  int retcode = 0;
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];


  if(gotsocket)
    *gotsocket = FALSE;

  if(!GOOD_MULTI_HANDLE(multi))
  if(!GOOD_MULTI_HANDLE(multi))
    return CURLM_BAD_HANDLE;
    return CURLM_BAD_HANDLE;


@@ -1135,9 +1139,21 @@ CURLMcode curl_multi_wait(struct Curl_multi *multi,
    free(ufds);
    free(ufds);
  if(ret)
  if(ret)
    *ret = retcode;
    *ret = retcode;
  if(gotsocket && (extra_fds || curlfds))
    /* if any socket was checked */
    *gotsocket = TRUE;

  return CURLM_OK;
  return CURLM_OK;
}
}


CURLMcode curl_multi_wait(struct Curl_multi *multi,
                          struct curl_waitfd extra_fds[],
                          unsigned int extra_nfds,
                          int timeout_ms,
                          int *ret)
{
  return Curl_multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, NULL);
}
/*
/*
 * Curl_multi_connchanged() is called to tell that there is a connection in
 * Curl_multi_connchanged() is called to tell that there is a connection in
 * this multi handle that has changed state (pipelining become possible, the
 * this multi handle that has changed state (pipelining become possible, the
+8 −0
Original line number Original line Diff line number Diff line
@@ -97,4 +97,12 @@ void Curl_multi_closed(struct Curl_easy *data, curl_socket_t s);
CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
                                 struct Curl_easy *data,
                                 struct Curl_easy *data,
                                 struct connectdata *conn);
                                 struct connectdata *conn);

CURLMcode Curl_multi_wait(struct Curl_multi *multi,
                          struct curl_waitfd extra_fds[],
                          unsigned int extra_nfds,
                          int timeout_ms,
                          int *ret,
                          bool *gotsocket); /* if any socket was checked */

#endif /* HEADER_CURL_MULTIIF_H */
#endif /* HEADER_CURL_MULTIIF_H */