[nginx] Resolver: properly handle connect() failures.

Ruslan Ermilov ru at nginx.com
Fri Feb 21 07:55:40 UTC 2014


details:   http://hg.nginx.org/nginx/rev/545a4d393e2f
branches:  
changeset: 5582:545a4d393e2f
user:      Ruslan Ermilov <ru at nginx.com>
date:      Thu Feb 20 17:27:09 2014 +0400
description:
Resolver: properly handle connect() failures.

If initial attempt to connect() the UDP socket failed, e.g.
due to network unreachable, no further attempts were made.

diffstat:

 src/core/ngx_resolver.c |  22 +++++++++++-----------
 1 files changed, 11 insertions(+), 11 deletions(-)

diffs (54 lines):

diff -r 4dee5ad51e9e -r 545a4d393e2f src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c	Thu Feb 20 13:48:40 2014 +0400
+++ b/src/core/ngx_resolver.c	Thu Feb 20 17:27:09 2014 +0400
@@ -3037,14 +3037,7 @@ ngx_udp_connect(ngx_udp_connection_t *uc
         ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
                       ngx_nonblocking_n " failed");
 
-        ngx_free_connection(c);
-
-        if (ngx_close_socket(s) == -1) {
-            ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
-                          ngx_close_socket_n " failed");
-        }
-
-        return NGX_ERROR;
+        goto failed;
     }
 
     rev = c->read;
@@ -3079,7 +3072,7 @@ ngx_udp_connect(ngx_udp_connection_t *uc
         ngx_log_error(NGX_LOG_CRIT, &uc->log, ngx_socket_errno,
                       "connect() failed");
 
-        return NGX_ERROR;
+        goto failed;
     }
 
     /* UDP sockets are always ready to write */
@@ -3093,16 +3086,23 @@ ngx_udp_connect(ngx_udp_connection_t *uc
                     /* eventport event type has no meaning: oneshot only */
 
         if (ngx_add_event(rev, NGX_READ_EVENT, event) != NGX_OK) {
-            return NGX_ERROR;
+            goto failed;
         }
 
     } else {
         /* rtsig */
 
         if (ngx_add_conn(c) == NGX_ERROR) {
-            return NGX_ERROR;
+            goto failed;
         }
     }
 
     return NGX_OK;
+
+failed:
+
+    ngx_close_connection(c);
+    uc->connection = NULL;
+
+    return NGX_ERROR;
 }



More information about the nginx-devel mailing list