]> granicus.if.org Git - apache/commitdiff
mpm_event: use a mutex for ptrans' allocator to be safe with concurrent
authorYann Ylavic <ylavic@apache.org>
Mon, 20 Feb 2017 13:38:03 +0000 (13:38 +0000)
committerYann Ylavic <ylavic@apache.org>
Mon, 20 Feb 2017 13:38:03 +0000 (13:38 +0000)
creation and destruction of its subpools, like with mod_http2.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1783755 13f79535-47bb-0310-9956-ffa450edef68

server/mpm/event/event.c

index 0910c4a960f56ef2d415028f272e6854dc61266f..151c920b0a04e4c7199da4a95d549e3caa40c706 100644 (file)
@@ -2096,6 +2096,7 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t * thd, void *dummy)
                 }
                 if (!listeners_disabled) {
                     void *csd = NULL;
+                    apr_thread_mutex_t *mutex;
                     ap_listen_rec *lr = (ap_listen_rec *) pt->baton;
                     apr_pool_t *ptrans;         /* Pool for per-transaction stuff */
                     ap_pop_pool(&ptrans, worker_queue_info);
@@ -2105,20 +2106,44 @@ static void * APR_THREAD_FUNC listener_thread(apr_thread_t * thd, void *dummy)
                         apr_allocator_t *allocator;
 
                         apr_allocator_create(&allocator);
-                        apr_allocator_max_free_set(allocator,
-                                                   ap_max_mem_free);
+                        apr_allocator_max_free_set(allocator, ap_max_mem_free);
                         apr_pool_create_ex(&ptrans, pconf, NULL, allocator);
-                        apr_allocator_owner_set(allocator, ptrans);
                         if (ptrans == NULL) {
                             ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
                                          ap_server_conf, APLOGNO(03097)
                                          "Failed to create transaction pool");
+                            apr_allocator_destroy(allocator);
                             signal_threads(ST_GRACEFUL);
                             return NULL;
                         }
+                        apr_allocator_owner_set(allocator, ptrans);
                     }
                     apr_pool_tag(ptrans, "transaction");
 
+                    /* We need a mutex in the allocator to synchronize ptrans'
+                     * children creations/destructions, but this mutex ought to
+                     * live in ptrans itself to avoid leaks, hence it's cleared
+                     * in ap_push_pool(). We could recycle some pconf's mutexes
+                     * like we do for ptrans subpools, but that'd need another
+                     * synchronization mechanism, whereas creating a pthread
+                     * mutex (unix here!) is really as simple/fast as a static
+                     * PTHREAD_MUTEX_INIT assignment, so let's not bother and
+                     * create the mutex for each ptrans (recycled or not).
+                     */
+                    rc = apr_thread_mutex_create(&mutex,
+                                                 APR_THREAD_MUTEX_DEFAULT,
+                                                 ptrans);
+                    if (rc != APR_SUCCESS) {
+                        ap_log_error(APLOG_MARK, APLOG_CRIT, rc,
+                                     ap_server_conf, APLOGNO()
+                                     "Failed to create transaction pool mutex");
+                        ap_push_pool(worker_queue_info, ptrans);
+                        signal_threads(ST_GRACEFUL);
+                        return NULL;
+                    }
+                    apr_allocator_mutex_set(apr_pool_allocator_get(ptrans),
+                                            mutex);
+
                     get_worker(&have_idle_worker, 1, &workers_were_busy);
                     rc = lr->accept_func(&csd, lr, ptrans);