Port the TCP socket fix made in ares_fds() to ares_getsock() as well.
authorSteinar H. Gunderson <sesse@google.com>
Sat, 29 Sep 2007 19:26:59 +0000 (19:26 +0000)
committerSteinar H. Gunderson <sesse@google.com>
Sat, 29 Sep 2007 19:26:59 +0000 (19:26 +0000)
ares_getsock.c

index 99419ae..38b8fef 100644 (file)
@@ -34,16 +34,18 @@ int ares_getsock(ares_channel channel,
 
   ares_socket_t *socks = (ares_socket_t *)s;
 
-  /* No queries, no file descriptors. */
-  if (ares__is_list_empty(&(channel->all_queries)))
-    return 0;
+  /* Are there any active queries? */
+  int active_queries = !ares__is_list_empty(&(channel->all_queries));
 
   for (i = 0;
        (i < channel->nservers) && (sockindex < ARES_GETSOCK_MAXNUM);
        i++)
     {
       server = &channel->servers[i];
-      if (server->udp_socket != ARES_SOCKET_BAD)
+      /* We only need to register interest in UDP sockets if we have
+       * outstanding queries.
+       */
+      if (active_queries && server->udp_socket != ARES_SOCKET_BAD)
         {
           if(sockindex >= numsocks)
             break;
@@ -51,6 +53,10 @@ int ares_getsock(ares_channel channel,
           bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
           sockindex++;
         }
+      /* We always register for TCP events, because we want to know
+       * when the other side closes the connection, so we don't waste
+       * time trying to use a broken connection.
+       */
       if (server->tcp_socket != ARES_SOCKET_BAD)
        {
          if(sockindex >= numsocks)
@@ -58,7 +64,7 @@ int ares_getsock(ares_channel channel,
          socks[sockindex] = server->tcp_socket;
          bitmap |= ARES_GETSOCK_READABLE(setbits, sockindex);
 
-         if (server->qhead)
+         if (server->qhead && active_queries)
            /* then the tcp socket is also writable! */
            bitmap |= ARES_GETSOCK_WRITABLE(setbits, sockindex);