curl / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] ares: better error return on timeouts

From: Daniel Stenberg <daniel_at_haxx.se>
Date: Mon, 6 Mar 2017 22:45:10 +0100

Reported-by: Ray Satiro

Bug: https://curl.haxx.se/mail/lib-2017-03/0009.html

---
  lib/asyn-ares.c | 21 +++++++++------------
  1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/lib/asyn-ares.c b/lib/asyn-ares.c
index ae73d0502..cd847df47 100644
--- a/lib/asyn-ares.c
+++ b/lib/asyn-ares.c
@@ -366,11 +366,11 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
    }
    if(!timeout)
      timeout = CURL_TIMEOUT_RESOLVE * 1000; /* default name resolve timeout */
    /* Wait for the name resolve query to complete. */
-  for(;;) {
+  while(!result) {
      struct timeval *tvp, tv, store;
      long timediff;
      int itimeout;
      int timeout_ms;
@@ -388,32 +388,29 @@ CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
        timeout_ms = (int)(tvp->tv_usec/1000);
      else
        timeout_ms = 1000;
      waitperform(conn, timeout_ms);
-    Curl_resolver_is_resolved(conn, &temp_entry);
+    result = Curl_resolver_is_resolved(conn, &temp_entry);
-    if(conn->async.done)
+    if(result || conn->async.done)
        break;
-    if(Curl_pgrsUpdate(conn)) {
+    if(Curl_pgrsUpdate(conn))
        result = CURLE_ABORTED_BY_CALLBACK;
-      timeout = -1; /* trigger the cancel below */
-    }
      else {
        struct timeval now2 = Curl_tvnow();
        timediff = Curl_tvdiff(now2, now); /* spent time */
        timeout -= timediff?timediff:1; /* always deduct at least 1 */
        now = now2; /* for next loop */
      }
-
-    if(timeout < 0) {
-      /* failure, so we cancel the ares operation */
-      ares_cancel((ares_channel)data->state.resolver);
-      break;
-    }
+    if(timeout < 0)
+      result = CURLE_OPERATION_TIMEDOUT;
    }
+  if(result)
+    /* our timeout, so we cancel the ares operation */
+    ares_cancel((ares_channel)data->state.resolver);
    /* Operation complete, if the lookup was successful we now have the entry
       in the cache. */
    if(entry)
      *entry = conn->async.dns;
-- 
2.11.0
-- 
  / daniel.haxx.se
--1129329158-964808630-1488836926=:27614
Content-Type: text/x-diff; name=0001-ares-better-error-return-on-timeouts.patch
Content-Transfer-Encoding: BASE64
Content-ID: <alpine.DEB.2.20.1703062248460.27614_at_tvnag.unkk.fr>
Content-Description: 
Content-Disposition: attachment; filename=0001-ares-better-error-return-on-timeouts.patch
RnJvbSA5YTg2M2M5Y2VlNjE4Y2I0N2U5NjllYzZmYmYxYTU2M2VkZjgwNzg3
IE1vbiBTZXAgMTcgMDA6MDA6MDAgMjAwMQ0KRnJvbTogRGFuaWVsIFN0ZW5i
ZXJnIDxkYW5pZWxAaGF4eC5zZT4NCkRhdGU6IE1vbiwgNiBNYXIgMjAxNyAy
Mjo0NToxMCArMDEwMA0KU3ViamVjdDogW1BBVENIXSBhcmVzOiBiZXR0ZXIg
ZXJyb3IgcmV0dXJuIG9uIHRpbWVvdXRzDQoNClJlcG9ydGVkLWJ5OiBSYXkg
U2F0aXJvDQoNCkJ1ZzogaHR0cHM6Ly9jdXJsLmhheHguc2UvbWFpbC9saWIt
MjAxNy0wMy8wMDA5Lmh0bWwNCi0tLQ0KIGxpYi9hc3luLWFyZXMuYyB8IDIx
ICsrKysrKysrKy0tLS0tLS0tLS0tLQ0KIDEgZmlsZSBjaGFuZ2VkLCA5IGlu
c2VydGlvbnMoKyksIDEyIGRlbGV0aW9ucygtKQ0KDQpkaWZmIC0tZ2l0IGEv
bGliL2FzeW4tYXJlcy5jIGIvbGliL2FzeW4tYXJlcy5jDQppbmRleCBhZTcz
ZDA1MDIuLmNkODQ3ZGY0NyAxMDA2NDQNCi0tLSBhL2xpYi9hc3luLWFyZXMu
Yw0KKysrIGIvbGliL2FzeW4tYXJlcy5jDQpAQCAtMzY2LDExICszNjYsMTEg
QEAgQ1VSTGNvZGUgQ3VybF9yZXNvbHZlcl93YWl0X3Jlc29sdihzdHJ1Y3Qg
Y29ubmVjdGRhdGEgKmNvbm4s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Ag
ICAqZW50cnkgPSBjb25uLT5hc3luYy5kbnM7DQotLSANCjIuMTEuMA0KDQo=
--1129329158-964808630-1488836926=:27614
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLQpVbnN1YnNjcmliZTogaHR0cHM6Ly9jb29sLmhheHguc2UvbGlzdC9saXN0aW5m
by9jdXJsLWxpYnJhcnkKRXRpcXVldHRlOiAgIGh0dHBzOi8vY3VybC5oYXh4LnNlL21haWwvZXRp
cXVldHRlLmh0bWw=
--1129329158-964808630-1488836926=:27614--
Received on 2001-09-17