Hello All,

After spending quite some time on apache and components ( specifically
mod_proxy_http, mod_mem_cache etc) we have noticed some memory leaks which
we are publishing with attached patch.

The leak can be observed when requests are sent on keep-alive connection
without closing the connection. As long as the connection is open, in apache
we tend to allocate some memory from connection pool for processing each
request. So when you have more requests per connection you tend to see
apache consuming more memory. If we release the connection and we see that
apache frees the memory and don't see this leaks impact.

I have also attached plots ( thanks to Paul for good article
http://journal.paul.querna.org/articles/2005/02/23/apr-memory-pools-rock/ )
on memory utilization of apr pools which clearly show the issue. The plots
are drawn using apr pool logs collected with and without the attached patch
for 25K requests on one connection.

Please help us to know if there is any issue with this patch, we have found
this patch working at high loads for long time period confirming the patch
doesn't break code and also see that without closing the connection with
more requests we don;t see any big time leaks.

But we are not sure if this patch breaks any of the features apache has
implemented, which we are not aware of.

thanks to every on apache support.

-regards
Harish
==== //depot/httproxy/httpd-2.2.9/modules/proxy/mod_proxy_http.c#4 - 
/home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/modules/proxy/mod_proxy_http.c
 ====
--- /tmp/tmp.30411.87   2008-10-14 19:59:29.000000000 +0530
+++ 
/home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/modules/proxy/mod_proxy_http.c
       2008-10-14 19:35:39.000000000 +0530
@@ -1895,9 +1895,12 @@ static int proxy_http_handler(request_re
      * connection ID of the current upstream connection is the same as that
      * of the connection when the socket was opened.
      */
-    apr_pool_t *p = r->connection->pool;
+    // Fix for bugId:9
+    // The allocation from connection pool to request pool was done
+    // to avoid request processing allocating memory from connection pool.
+    apr_pool_t *p = r->pool;
     conn_rec *c = r->connection;
-    apr_uri_t *uri = apr_palloc(r->connection->pool, sizeof(*uri));
+    apr_uri_t *uri = apr_palloc(r->pool, sizeof(*uri));
 
     /* find the scheme */
     u = strchr(url, ':');
==== //depot/httproxy/httpd-2.2.9/server/mpm/experimental/event/event.c#1 - 
/home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/server/mpm/experimental/event/event.c
 ====
--- /tmp/tmp.30411.142  2008-10-14 19:59:29.000000000 +0530
+++ 
/home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/server/mpm/experimental/event/event.c
        2008-10-14 19:50:53.000000000 +0530
@@ -566,9 +566,18 @@ static int process_socket(apr_pool_t * p
     int csd;
     int rc;
     apr_time_t time_now = 0;
-    ap_sb_handle_t *sbh;
+    ap_sb_handle_t *sbh = NULL;
 
+    // Fix for bugId:9
+    // memory from connection pool was getting used every time
+    // an new request is served and if case if the connection is keep-alive
+    // this memory never gets freed and we see a leak.
+    // The fix would help allocation of memory to hold child_num and thread_num
+    // in ap_create_sb_handle once in life time of connection
+#if 0
     ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
+#endif /* 0 */
+    
     apr_os_sock_get(&csd, sock);
 
     time_now = apr_time_now();
@@ -577,6 +586,9 @@ static int process_socket(apr_pool_t * p
 
         cs = apr_pcalloc(p, sizeof(conn_state_t));
 
+        // Fix for bugId:9
+        ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
+
         pt = apr_pcalloc(p, sizeof(*pt));
 
         cs->bucket_alloc = apr_bucket_alloc_create(p);
@@ -620,7 +632,12 @@ static int process_socket(apr_pool_t * p
     }
     else {
         c = cs->c;
+        // Fix for bugId:9
+#if 0
         c->sbh = sbh;
+#endif /* 0 */
+        sbh =  c->sbh;
+        ap_create_sb_handle(&sbh, p, my_child_num, my_thread_num);
     }
 
     if (c->clogging_input_filters && !c->aborted) {
==== //depot/httproxy/httpd-2.2.9/server/scoreboard.c#1 - 
/home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/server/scoreboard.c ====
--- /tmp/tmp.30411.190  2008-10-14 19:59:29.000000000 +0530
+++ /home/harish/harish-desktop/depot/httproxy/httpd-2.2.9/server/scoreboard.c  
2008-10-14 19:25:18.000000000 +0530
@@ -378,7 +378,10 @@ int find_child_by_pid(apr_proc_t *pid)
 AP_DECLARE(void) ap_create_sb_handle(ap_sb_handle_t **new_sbh, apr_pool_t *p,
                                      int child_num, int thread_num)
 {
-    *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
+    // fix for bugId: 9
+    if ( *new_sbh == NULL )
+      *new_sbh = (ap_sb_handle_t *)apr_palloc(p, sizeof(ap_sb_handle_t));
+    // ends of fix
     (*new_sbh)->child_num = child_num;
     (*new_sbh)->thread_num = thread_num;
 }

Reply via email to