diff src/http/modules/proxy/ngx_http_proxy_handler.c @ 377:41437e4fd9b4

nginx-0.0.7-2004-07-05-19:08:23 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 05 Jul 2004 15:08:23 +0000
parents 744ccb59062d
children 73688d5d7fc3
line wrap: on
line diff
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -608,6 +608,7 @@ void ngx_http_proxy_finalize_request(ngx
 
 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p)
 {
+    ngx_socket_t       fd;
     ngx_connection_t  *c;
 
     c = p->upstream->peer.connection;
@@ -650,12 +651,36 @@ void ngx_http_proxy_close_connection(ngx
         }
     }
 
-    if (ngx_close_socket(c->fd) == -1) {
+    /*
+     * we have to clean the connection information before the closing
+     * because another thread may reopen the same file descriptor
+     * before we clean the connection
+     */
+    
+    if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
+
+        if (c->read->prev) {
+            ngx_delete_posted_event(c->read);
+        }
+
+        if (c->write->prev) {
+            ngx_delete_posted_event(c->write);
+        }
+
+        c->read->closed = 1;
+        c->write->closed = 1;
+
+        ngx_mutex_unlock(ngx_posted_events_mutex);
+    }
+
+    fd = c->fd;
+    c->fd = (ngx_socket_t) -1;
+    c->data = NULL;
+
+    if (ngx_close_socket(fd) == -1) {
         ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                       ngx_close_socket_n " failed");
     }
-
-    c->fd = (ngx_socket_t) -1;
 }