ca8514
diff --git a/modules/proxy/mod_proxy.h b/modules/proxy/mod_proxy.h
ca8514
index 57cc92f..fbbd508 100644
ca8514
--- a/modules/proxy/mod_proxy.h
ca8514
+++ b/modules/proxy/mod_proxy.h
ca8514
@@ -288,12 +288,15 @@ typedef struct {
ca8514
 
ca8514
 /* Connection pool */
ca8514
 struct proxy_conn_pool {
ca8514
-    apr_pool_t     *pool;   /* The pool used in constructor and destructor calls */
ca8514
-    apr_sockaddr_t *addr;   /* Preparsed remote address info */
ca8514
-    apr_reslist_t  *res;    /* Connection resource list */
ca8514
-    proxy_conn_rec *conn;   /* Single connection for prefork mpm */
ca8514
+    apr_pool_t     *pool;     /* The pool used in constructor and destructor calls */
ca8514
+    apr_sockaddr_t *addr;     /* Preparsed remote address info */
ca8514
+    apr_reslist_t  *res;      /* Connection resource list */
ca8514
+    proxy_conn_rec *conn;     /* Single connection for prefork mpm */
ca8514
+    apr_pool_t     *dns_pool; /* The pool used for worker scoped DNS resolutions */
ca8514
 };
ca8514
 
ca8514
+#define AP_VOLATILIZE_T(T, x) (*(T volatile *)&(x))
ca8514
+
ca8514
 /* worker status bits */
ca8514
 /*
ca8514
  * NOTE: Keep up-to-date w/ proxy_wstat_tbl[]
ca8514
@@ -475,7 +478,9 @@ struct proxy_worker {
ca8514
     proxy_conn_pool     *cp;    /* Connection pool to use */
ca8514
     proxy_worker_shared   *s;   /* Shared data */
ca8514
     proxy_balancer  *balancer;  /* which balancer am I in? */
ca8514
+#if APR_HAS_THREADS
ca8514
     apr_thread_mutex_t  *tmutex; /* Thread lock for updating address cache */
ca8514
+#endif
ca8514
     void            *context;   /* general purpose storage */
ca8514
     ap_conf_vector_t *section_config; /* <Proxy>-section wherein defined */
ca8514
 };
ca8514
@@ -534,7 +539,9 @@ struct proxy_balancer {
ca8514
     apr_time_t      wupdated;    /* timestamp of last change to workers list */
ca8514
     proxy_balancer_method *lbmethod;
ca8514
     apr_global_mutex_t  *gmutex; /* global lock for updating list of workers */
ca8514
+#if APR_HAS_THREADS
ca8514
     apr_thread_mutex_t  *tmutex; /* Thread lock for updating shm */
ca8514
+#endif
ca8514
     proxy_server_conf *sconf;
ca8514
     void            *context;    /* general purpose storage */
ca8514
     proxy_balancer_shared *s;    /* Shared data */
ca8514
diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c
ca8514
index c59f5e9..3a28038 100644
ca8514
--- a/modules/proxy/mod_proxy_balancer.c
ca8514
+++ b/modules/proxy/mod_proxy_balancer.c
ca8514
@@ -346,23 +346,27 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
ca8514
     proxy_worker *candidate = NULL;
ca8514
     apr_status_t rv;
ca8514
 
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01163)
ca8514
                       "%s: Lock failed for find_best_worker()",
ca8514
                       balancer->s->name);
ca8514
         return NULL;
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     candidate = (*balancer->lbmethod->finder)(balancer, r);
ca8514
 
ca8514
     if (candidate)
ca8514
         candidate->s->elected++;
ca8514
 
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01164)
ca8514
                       "%s: Unlock failed for find_best_worker()",
ca8514
                       balancer->s->name);
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     if (candidate == NULL) {
ca8514
         /* All the workers are in error state or disabled.
ca8514
@@ -492,11 +496,13 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
ca8514
     /* Step 2: Lock the LoadBalancer
ca8514
      * XXX: perhaps we need the process lock here
ca8514
      */
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_LOCK(*balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01166)
ca8514
                       "%s: Lock failed for pre_request", (*balancer)->s->name);
ca8514
         return DECLINED;
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     /* Step 3: force recovery */
ca8514
     force_recovery(*balancer, r->server);
ca8514
@@ -557,20 +563,24 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
ca8514
             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01167)
ca8514
                           "%s: All workers are in error state for route (%s)",
ca8514
                           (*balancer)->s->name, route);
ca8514
+#if APR_HAS_THREADS
ca8514
             if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
ca8514
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01168)
ca8514
                               "%s: Unlock failed for pre_request",
ca8514
                               (*balancer)->s->name);
ca8514
             }
ca8514
+#endif
ca8514
             return HTTP_SERVICE_UNAVAILABLE;
ca8514
         }
ca8514
     }
ca8514
 
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_UNLOCK(*balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01169)
ca8514
                       "%s: Unlock failed for pre_request",
ca8514
                       (*balancer)->s->name);
ca8514
     }
ca8514
+#endif
ca8514
     if (!*worker) {
ca8514
         runtime = find_best_worker(*balancer, r);
ca8514
         if (!runtime) {
ca8514
@@ -644,12 +654,14 @@ static int proxy_balancer_post_request(proxy_worker *worker,
ca8514
 
ca8514
     apr_status_t rv;
ca8514
 
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01173)
ca8514
                       "%s: Lock failed for post_request",
ca8514
                       balancer->s->name);
ca8514
         return HTTP_INTERNAL_SERVER_ERROR;
ca8514
     }
ca8514
+#endif
ca8514
 
ca8514
     if (!apr_is_empty_array(balancer->errstatuses)
ca8514
         && !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
ca8514
@@ -681,11 +693,12 @@ static int proxy_balancer_post_request(proxy_worker *worker,
ca8514
         worker->s->error_time = apr_time_now();
ca8514
 
ca8514
     }
ca8514
-
ca8514
+#if APR_HAS_THREADS
ca8514
     if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
ca8514
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01175)
ca8514
                       "%s: Unlock failed for post_request", balancer->s->name);
ca8514
     }
ca8514
+#endif
ca8514
     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01176)
ca8514
                   "proxy_balancer_post_request for (%s)", balancer->s->name);
ca8514
 
ca8514
@@ -945,7 +958,6 @@ static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
ca8514
             PROXY_STRNCPY(balancer->s->sname, sname); /* We know this will succeed */
ca8514
 
ca8514
             balancer->max_workers = balancer->workers->nelts + balancer->growth;
ca8514
-
ca8514
             /* Create global mutex */
ca8514
             rv = ap_global_mutex_create(&(balancer->gmutex), NULL, balancer_mutex_type,
ca8514
                                         balancer->s->sname, s, pconf, 0);
ca8514
@@ -955,7 +967,6 @@ static int balancer_post_config(apr_pool_t *pconf, apr_pool_t *plog,
ca8514
                              balancer->s->sname);
ca8514
                 return HTTP_INTERNAL_SERVER_ERROR;
ca8514
             }
ca8514
-
ca8514
             apr_pool_cleanup_register(pconf, (void *)s, lock_remove,
ca8514
                                       apr_pool_cleanup_null);
ca8514
 
ca8514
@@ -1135,17 +1146,21 @@ static int balancer_handler(request_rec *r)
ca8514
 
ca8514
     balancer = (proxy_balancer *)conf->balancers->elts;
ca8514
     for (i = 0; i < conf->balancers->nelts; i++, balancer++) {
ca8514
+#if APR_HAS_THREADS
ca8514
         if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
ca8514
             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01189)
ca8514
                           "%s: Lock failed for balancer_handler",
ca8514
                           balancer->s->name);
ca8514
         }
ca8514
+#endif
ca8514
         ap_proxy_sync_balancer(balancer, r->server, conf);
ca8514
+#if APR_HAS_THREADS
ca8514
         if ((rv = PROXY_THREAD_UNLOCK(balancer)) != APR_SUCCESS) {
ca8514
             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01190)
ca8514
                           "%s: Unlock failed for balancer_handler",
ca8514
                           balancer->s->name);
ca8514
         }
ca8514
+#endif
ca8514
     }
ca8514
 
ca8514
     if (r->args && (r->method_number == M_GET)) {
ca8514
@@ -1359,11 +1374,13 @@ static int balancer_handler(request_rec *r)
ca8514
             proxy_worker *nworker;
ca8514
             nworker = ap_proxy_get_worker(r->pool, bsel, conf, val);
ca8514
             if (!nworker && storage->num_free_slots(bsel->wslot)) {
ca8514
+#if APR_HAS_THREADS
ca8514
                 if ((rv = PROXY_GLOBAL_LOCK(bsel)) != APR_SUCCESS) {
ca8514
                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01194)
ca8514
                                   "%s: Lock failed for adding worker",
ca8514
                                   bsel->s->name);
ca8514
                 }
ca8514
+#endif
ca8514
                 ret = ap_proxy_define_worker(conf->pool, &nworker, bsel, conf, val, 0);
ca8514
                 if (!ret) {
ca8514
                     unsigned int index;
ca8514
@@ -1372,53 +1389,76 @@ static int balancer_handler(request_rec *r)
ca8514
                     if ((rv = storage->grab(bsel->wslot, &index)) != APR_SUCCESS) {
ca8514
                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01195)
ca8514
                                       "worker slotmem_grab failed");
ca8514
+#if APR_HAS_THREADS
ca8514
                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ca8514
                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01196)
ca8514
                                           "%s: Unlock failed for adding worker",
ca8514
                                           bsel->s->name);
ca8514
                         }
ca8514
+#endif
ca8514
                         return HTTP_BAD_REQUEST;
ca8514
                     }
ca8514
                     if ((rv = storage->dptr(bsel->wslot, index, (void *)&shm)) != APR_SUCCESS) {
ca8514
                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01197)
ca8514
                                       "worker slotmem_dptr failed");
ca8514
+#if APR_HAS_THREADS
ca8514
                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ca8514
                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01198)
ca8514
                                           "%s: Unlock failed for adding worker",
ca8514
                                           bsel->s->name);
ca8514
                         }
ca8514
+#endif
ca8514
                         return HTTP_BAD_REQUEST;
ca8514
                     }
ca8514
                     if ((rv = ap_proxy_share_worker(nworker, shm, index)) != APR_SUCCESS) {
ca8514
                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01199)
ca8514
                                       "Cannot share worker");
ca8514
+#if APR_HAS_THREADS
ca8514
                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ca8514
                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01200)
ca8514
                                           "%s: Unlock failed for adding worker",
ca8514
                                           bsel->s->name);
ca8514
                         }
ca8514
+#endif
ca8514
                         return HTTP_BAD_REQUEST;
ca8514
                     }
ca8514
                     if ((rv = ap_proxy_initialize_worker(nworker, r->server, conf->pool)) != APR_SUCCESS) {
ca8514
                         ap_log_rerror(APLOG_MARK, APLOG_EMERG, rv, r, APLOGNO(01201)
ca8514
                                       "Cannot init worker");
ca8514
+#if APR_HAS_THREADS
ca8514
                         if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ca8514
                             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01202)
ca8514
                                           "%s: Unlock failed for adding worker",
ca8514
                                           bsel->s->name);
ca8514
                         }
ca8514
+#endif
ca8514
                         return HTTP_BAD_REQUEST;
ca8514
                     }
ca8514
                     /* sync all timestamps */
ca8514
                     bsel->wupdated = bsel->s->wupdated = nworker->s->updated = apr_time_now();
ca8514
                     /* by default, all new workers are disabled */
ca8514
                     ap_proxy_set_wstatus(PROXY_WORKER_DISABLED_FLAG, 1, nworker);
ca8514
+                } else {
ca8514
+                            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10163)
ca8514
+                                  "%s: failed to add worker %s",
ca8514
+                                  bsel->s->name, val);
ca8514
+#if APR_HAS_THREADS
ca8514
+                    PROXY_GLOBAL_UNLOCK(bsel);
ca8514
+#endif
ca8514
+                    return HTTP_BAD_REQUEST;
ca8514
                 }
ca8514
+#if APR_HAS_THREADS
ca8514
                 if ((rv = PROXY_GLOBAL_UNLOCK(bsel)) != APR_SUCCESS) {
ca8514
                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01203)
ca8514
                                   "%s: Unlock failed for adding worker",
ca8514
                                   bsel->s->name);
ca8514
                 }
ca8514
+#endif
ca8514
+            } else {
ca8514
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10164)
ca8514
+                                  "%s: failed to add worker %s",
ca8514
+                                  bsel->s->name, val);
ca8514
+                return HTTP_BAD_REQUEST;
ca8514
             }
ca8514
 
ca8514
         }
ca8514
diff --git a/modules/proxy/mod_proxy_ftp.c b/modules/proxy/mod_proxy_ftp.c
ca8514
index 5d9175e..5c4d641 100644
ca8514
--- a/modules/proxy/mod_proxy_ftp.c
ca8514
+++ b/modules/proxy/mod_proxy_ftp.c
ca8514
@@ -979,8 +979,10 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
ca8514
     apr_status_t rv;
ca8514
     conn_rec *origin, *data = NULL;
ca8514
     apr_status_t err = APR_SUCCESS;
ca8514
+#if APR_HAS_THREADS
ca8514
     apr_status_t uerr = APR_SUCCESS;
ca8514
-    apr_bucket_brigade *bb = apr_brigade_create(p, c->bucket_alloc);
ca8514
+#endif
ca8514
+    apr_bucket_brigade *bb;
ca8514
     char *buf, *connectname;
ca8514
     apr_port_t connectport;
ca8514
     char *ftpmessage = NULL;
ca8514
@@ -1120,13 +1122,15 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
ca8514
 
ca8514
     if (worker->s->is_address_reusable) {
ca8514
         if (!worker->cp->addr) {
ca8514
+#if APR_HAS_THREADS
ca8514
             if ((err = PROXY_THREAD_LOCK(worker->balancer)) != APR_SUCCESS) {
ca8514
                 ap_log_rerror(APLOG_MARK, APLOG_ERR, err, r, APLOGNO(01037) "lock");
ca8514
                 return HTTP_INTERNAL_SERVER_ERROR;
ca8514
             }
ca8514
+#endif
ca8514
         }
ca8514
-        connect_addr = worker->cp->addr;
ca8514
-        address_pool = worker->cp->pool;
ca8514
+        connect_addr = AP_VOLATILIZE_T(apr_sockaddr_t *, worker->cp->addr);
ca8514
+        address_pool = worker->cp->dns_pool;
ca8514
     }
ca8514
     else
ca8514
         address_pool = r->pool;
ca8514
@@ -1139,9 +1143,11 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
ca8514
                                     address_pool);
ca8514
     if (worker->s->is_address_reusable && !worker->cp->addr) {
ca8514
         worker->cp->addr = connect_addr;
ca8514
+#if APR_HAS_THREADS
ca8514
         if ((uerr = PROXY_THREAD_UNLOCK(worker->balancer)) != APR_SUCCESS) {
ca8514
             ap_log_rerror(APLOG_MARK, APLOG_ERR, uerr, r, APLOGNO(01038) "unlock");
ca8514
         }
ca8514
+#endif
ca8514
     }
ca8514
     /*
ca8514
      * get all the possible IP addresses for the destname and loop through
ca8514
@@ -1212,6 +1218,7 @@ static int proxy_ftp_handler(request_rec *r, proxy_worker *worker,
ca8514
      * correct directory...
ca8514
      */
ca8514
 
ca8514
+    bb = apr_brigade_create(p, c->bucket_alloc);
ca8514
 
ca8514
     /* possible results: */
ca8514
     /* 120 Service ready in nnn minutes. */
ca8514
diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c
ca8514
index 2bfc8f0..7714b6c 100644
ca8514
--- a/modules/proxy/proxy_util.c
ca8514
+++ b/modules/proxy/proxy_util.c
ca8514
@@ -1167,8 +1167,10 @@ PROXY_DECLARE(char *) ap_proxy_define_balancer(apr_pool_t *p,
ca8514
     lbmethod = ap_lookup_provider(PROXY_LBMETHOD, "byrequests", "0");
ca8514
 
ca8514
     (*balancer)->workers = apr_array_make(p, 5, sizeof(proxy_worker *));
ca8514
+#if APR_HAS_THREADS
ca8514
     (*balancer)->gmutex = NULL;
ca8514
     (*balancer)->tmutex = NULL;
ca8514
+#endif
ca8514
     (*balancer)->lbmethod = lbmethod;
ca8514
 
ca8514
     if (do_malloc)
ca8514
@@ -1257,7 +1259,9 @@ PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
ca8514
 
ca8514
 PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer, server_rec *s, apr_pool_t *p)
ca8514
 {
ca8514
+#if APR_HAS_THREADS
ca8514
     apr_status_t rv = APR_SUCCESS;
ca8514
+#endif
ca8514
     ap_slotmem_provider_t *storage = balancer->storage;
ca8514
     apr_size_t size;
ca8514
     unsigned int num;
ca8514
@@ -1297,6 +1301,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balance
ca8514
     if (balancer->lbmethod && balancer->lbmethod->reset)
ca8514
         balancer->lbmethod->reset(balancer, s);
ca8514
 
ca8514
+#if APR_HAS_THREADS
ca8514
     if (balancer->tmutex == NULL) {
ca8514
         rv = apr_thread_mutex_create(&(balancer->tmutex), APR_THREAD_MUTEX_DEFAULT, p);
ca8514
         if (rv != APR_SUCCESS) {
ca8514
@@ -1305,6 +1310,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balance
ca8514
             return rv;
ca8514
         }
ca8514
     }
ca8514
+#endif
ca8514
     return APR_SUCCESS;
ca8514
 }
ca8514
 
ca8514
@@ -1446,16 +1452,14 @@ static void socket_cleanup(proxy_conn_rec *conn)
ca8514
 
ca8514
 static apr_status_t conn_pool_cleanup(void *theworker)
ca8514
 {
ca8514
-    proxy_worker *worker = (proxy_worker *)theworker;
ca8514
-    if (worker->cp->res) {
ca8514
-        worker->cp->pool = NULL;
ca8514
-    }
ca8514
+    ((proxy_worker *)theworker)->cp = NULL;
ca8514
     return APR_SUCCESS;
ca8514
 }
ca8514
 
ca8514
 static void init_conn_pool(apr_pool_t *p, proxy_worker *worker)
ca8514
 {
ca8514
     apr_pool_t *pool;
ca8514
+    apr_pool_t *dns_pool;
ca8514
     proxy_conn_pool *cp;
ca8514
 
ca8514
     /*
ca8514
@@ -1466,12 +1470,21 @@ static void init_conn_pool(apr_pool_t *p, proxy_worker *worker)
ca8514
      */
ca8514
     apr_pool_create(&pool, p);
ca8514
     apr_pool_tag(pool, "proxy_worker_cp");
ca8514
+    /*
ca8514
+     * Create a subpool of the connection pool for worker
ca8514
+     * scoped DNS resolutions. This is needed to avoid race
ca8514
+     * conditions in using the connection pool by multiple
ca8514
+     * threads during ramp up.
ca8514
+     */
ca8514
+    apr_pool_create(&dns_pool, pool);
ca8514
+    apr_pool_tag(dns_pool, "proxy_worker_dns");
ca8514
     /*
ca8514
      * Alloc from the same pool as worker.
ca8514
      * proxy_conn_pool is permanently attached to the worker.
ca8514
      */
ca8514
     cp = (proxy_conn_pool *)apr_pcalloc(p, sizeof(proxy_conn_pool));
ca8514
     cp->pool = pool;
ca8514
+    cp->dns_pool = dns_pool;
ca8514
     worker->cp = cp;
ca8514
 }
ca8514
 
ca8514
@@ -1487,14 +1500,6 @@ static apr_status_t connection_cleanup(void *theconn)
ca8514
     proxy_conn_rec *conn = (proxy_conn_rec *)theconn;
ca8514
     proxy_worker *worker = conn->worker;
ca8514
 
ca8514
-    /*
ca8514
-     * If the connection pool is NULL the worker
ca8514
-     * cleanup has been run. Just return.
ca8514
-     */
ca8514
-    if (!worker->cp->pool) {
ca8514
-        return APR_SUCCESS;
ca8514
-    }
ca8514
-
ca8514
     if (conn->r) {
ca8514
         apr_pool_destroy(conn->r->pool);
ca8514
         conn->r = NULL;
ca8514
@@ -1616,7 +1621,7 @@ static apr_status_t connection_destructor(void *resource, void *params,
ca8514
     proxy_worker *worker = params;
ca8514
 
ca8514
     /* Destroy the pool only if not called from reslist_destroy */
ca8514
-    if (worker->cp->pool) {
ca8514
+    if (worker->cp) {
ca8514
         proxy_conn_rec *conn = resource;
ca8514
         apr_pool_destroy(conn->pool);
ca8514
     }
ca8514
@@ -1972,67 +1977,73 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
ca8514
                      ap_proxy_worker_name(p, worker));
ca8514
     }
ca8514
     else {
ca8514
-        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00927)
ca8514
-                     "initializing worker %s local",
ca8514
-                     ap_proxy_worker_name(p, worker));
ca8514
         apr_global_mutex_lock(proxy_mutex);
ca8514
-        /* Now init local worker data */
ca8514
-        if (worker->tmutex == NULL) {
ca8514
-            rv = apr_thread_mutex_create(&(worker->tmutex), APR_THREAD_MUTEX_DEFAULT, p);
ca8514
-            if (rv != APR_SUCCESS) {
ca8514
-                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00928)
ca8514
-                             "can not create worker thread mutex");
ca8514
+        /* Check again after we got the lock if we are still uninitialized */
ca8514
+        if (!(AP_VOLATILIZE_T(unsigned int, worker->local_status) & PROXY_WORKER_INITIALIZED)) {
ca8514
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00927)
ca8514
+                         "initializing worker %s local",
ca8514
+                         ap_proxy_worker_name(p, worker));
ca8514
+            /* Now init local worker data */
ca8514
+#if APR_HAS_THREADS
ca8514
+            if (worker->tmutex == NULL) {
ca8514
+                rv = apr_thread_mutex_create(&(worker->tmutex), APR_THREAD_MUTEX_DEFAULT, p);
ca8514
+                if (rv != APR_SUCCESS) {
ca8514
+                    ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00928)
ca8514
+                                 "can not create worker thread mutex");
ca8514
+                    apr_global_mutex_unlock(proxy_mutex);
ca8514
+                    return rv;
ca8514
+                }
ca8514
+            }
ca8514
+#endif
ca8514
+            if (worker->cp == NULL)
ca8514
+                init_conn_pool(p, worker);
ca8514
+            if (worker->cp == NULL) {
ca8514
+                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00929)
ca8514
+                             "can not create connection pool");
ca8514
                 apr_global_mutex_unlock(proxy_mutex);
ca8514
-                return rv;
ca8514
+                return APR_EGENERAL;
ca8514
             }
ca8514
-        }
ca8514
-        if (worker->cp == NULL)
ca8514
-            init_conn_pool(p, worker);
ca8514
-        if (worker->cp == NULL) {
ca8514
-            ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00929)
ca8514
-                         "can not create connection pool");
ca8514
-            apr_global_mutex_unlock(proxy_mutex);
ca8514
-            return APR_EGENERAL;
ca8514
-        }
ca8514
 
ca8514
-        if (worker->s->hmax) {
ca8514
-            rv = apr_reslist_create(&(worker->cp->res),
ca8514
-                                    worker->s->min, worker->s->smax,
ca8514
-                                    worker->s->hmax, worker->s->ttl,
ca8514
-                                    connection_constructor, connection_destructor,
ca8514
-                                    worker, worker->cp->pool);
ca8514
+            if (worker->s->hmax) {
ca8514
+                rv = apr_reslist_create(&(worker->cp->res),
ca8514
+                                        worker->s->min, worker->s->smax,
ca8514
+                                        worker->s->hmax, worker->s->ttl,
ca8514
+                                        connection_constructor, connection_destructor,
ca8514
+                                        worker, worker->cp->pool);
ca8514
 
ca8514
-            apr_pool_cleanup_register(worker->cp->pool, (void *)worker,
ca8514
-                                      conn_pool_cleanup,
ca8514
-                                      apr_pool_cleanup_null);
ca8514
+                apr_pool_pre_cleanup_register(worker->cp->pool, worker,
ca8514
+                                              conn_pool_cleanup);
ca8514
 
ca8514
-            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00930)
ca8514
-                "initialized pool in child %" APR_PID_T_FMT " for (%s) min=%d max=%d smax=%d",
ca8514
-                 getpid(), worker->s->hostname_ex, worker->s->min,
ca8514
-                 worker->s->hmax, worker->s->smax);
ca8514
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00930)
ca8514
+                    "initialized pool in child %" APR_PID_T_FMT " for (%s) min=%d max=%d smax=%d",
ca8514
+                     getpid(), worker->s->hostname_ex, worker->s->min,
ca8514
+                     worker->s->hmax, worker->s->smax);
ca8514
 
ca8514
-            /* Set the acquire timeout */
ca8514
-            if (rv == APR_SUCCESS && worker->s->acquire_set) {
ca8514
-                apr_reslist_timeout_set(worker->cp->res, worker->s->acquire);
ca8514
-            }
ca8514
+                /* Set the acquire timeout */
ca8514
+                if (rv == APR_SUCCESS && worker->s->acquire_set) {
ca8514
+                    apr_reslist_timeout_set(worker->cp->res, worker->s->acquire);
ca8514
+                }
ca8514
 
ca8514
-        }
ca8514
-        else {
ca8514
-            void *conn;
ca8514
+            }
ca8514
+            else {
ca8514
+                void *conn;
ca8514
 
ca8514
-            rv = connection_constructor(&conn, worker, worker->cp->pool);
ca8514
-            worker->cp->conn = conn;
ca8514
+                rv = connection_constructor(&conn, worker, worker->cp->pool);
ca8514
+                worker->cp->conn = conn;
ca8514
 
ca8514
-            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00931)
ca8514
-                 "initialized single connection worker in child %" APR_PID_T_FMT " for (%s)",
ca8514
-                 getpid(), worker->s->hostname_ex);
ca8514
+                ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, s, APLOGNO(00931)
ca8514
+                     "initialized single connection worker in child %" APR_PID_T_FMT " for (%s)",
ca8514
+                     getpid(), worker->s->hostname_ex);
ca8514
+            }
ca8514
+            if (rv == APR_SUCCESS) {
ca8514
+                worker->local_status |= (PROXY_WORKER_INITIALIZED);
ca8514
+            }
ca8514
         }
ca8514
         apr_global_mutex_unlock(proxy_mutex);
ca8514
 
ca8514
     }
ca8514
     if (rv == APR_SUCCESS) {
ca8514
         worker->s->status |= (PROXY_WORKER_INITIALIZED);
ca8514
-        worker->local_status |= (PROXY_WORKER_INITIALIZED);
ca8514
     }
ca8514
     return rv;
ca8514
 }
ca8514
@@ -2292,13 +2303,13 @@ PROXY_DECLARE(int) ap_proxy_acquire_connection(const char *proxy_function,
ca8514
     else {
ca8514
         /* create the new connection if the previous was destroyed */
ca8514
         if (!worker->cp->conn) {
ca8514
-            connection_constructor((void **)conn, worker, worker->cp->pool);
ca8514
+            rv = connection_constructor((void **)conn, worker, worker->cp->pool);
ca8514
         }
ca8514
         else {
ca8514
             *conn = worker->cp->conn;
ca8514
             worker->cp->conn = NULL;
ca8514
+            rv = APR_SUCCESS;
ca8514
         }
ca8514
-        rv = APR_SUCCESS;
ca8514
     }
ca8514
 
ca8514
     if (rv != APR_SUCCESS) {
ca8514
@@ -2344,7 +2355,9 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
ca8514
 {
ca8514
     int server_port;
ca8514
     apr_status_t err = APR_SUCCESS;
ca8514
+#if APR_HAS_THREADS
ca8514
     apr_status_t uerr = APR_SUCCESS;
ca8514
+#endif
ca8514
     const char *uds_path;
ca8514
 
ca8514
     /*
ca8514
@@ -2481,25 +2494,39 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
ca8514
              * we can reuse the address.
ca8514
              */
ca8514
             if (!worker->cp->addr) {
ca8514
+#if APR_HAS_THREADS
ca8514
                 if ((err = PROXY_THREAD_LOCK(worker)) != APR_SUCCESS) {
ca8514
                     ap_log_rerror(APLOG_MARK, APLOG_ERR, err, r, APLOGNO(00945) "lock");
ca8514
                     return HTTP_INTERNAL_SERVER_ERROR;
ca8514
                 }
ca8514
+#endif
ca8514
 
ca8514
                 /*
ca8514
-                 * Worker can have the single constant backend address.
ca8514
-                 * The single DNS lookup is used once per worker.
ca8514
-                 * If dynamic change is needed then set the addr to NULL
ca8514
-                 * inside dynamic config to force the lookup.
ca8514
+                 * Recheck addr after we got the lock. This may have changed
ca8514
+                 * while waiting for the lock.
ca8514
                  */
ca8514
-                err = apr_sockaddr_info_get(&(worker->cp->addr),
ca8514
-                                            conn->hostname, APR_UNSPEC,
ca8514
-                                            conn->port, 0,
ca8514
-                                            worker->cp->pool);
ca8514
+                if (!AP_VOLATILIZE_T(apr_sockaddr_t *, worker->cp->addr)) {
ca8514
+
ca8514
+                    apr_sockaddr_t *addr;
ca8514
+
ca8514
+                    /*
ca8514
+                     * Worker can have the single constant backend address.
ca8514
+                     * The single DNS lookup is used once per worker.
ca8514
+                     * If dynamic change is needed then set the addr to NULL
ca8514
+                     * inside dynamic config to force the lookup.
ca8514
+                     */
ca8514
+                    err = apr_sockaddr_info_get(&addr,
ca8514
+                                                conn->hostname, APR_UNSPEC,
ca8514
+                                                conn->port, 0,
ca8514
+                                                worker->cp->dns_pool);
ca8514
+                    worker->cp->addr = addr;
ca8514
+                }
ca8514
                 conn->addr = worker->cp->addr;
ca8514
+#if APR_HAS_THREADS
ca8514
                 if ((uerr = PROXY_THREAD_UNLOCK(worker)) != APR_SUCCESS) {
ca8514
                     ap_log_rerror(APLOG_MARK, APLOG_ERR, uerr, r, APLOGNO(00946) "unlock");
ca8514
                 }
ca8514
+#endif
ca8514
             }
ca8514
             else {
ca8514
                 conn->addr = worker->cp->addr;
ca8514
@@ -3422,7 +3449,9 @@ PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b, server_rec
ca8514
             (*runtime)->cp = NULL;
ca8514
             (*runtime)->balancer = b;
ca8514
             (*runtime)->s = shm;
ca8514
+#if APR_HAS_THREADS
ca8514
             (*runtime)->tmutex = NULL;
ca8514
+#endif
ca8514
             rv = ap_proxy_initialize_worker(*runtime, s, conf->pool);
ca8514
             if (rv != APR_SUCCESS) {
ca8514
                 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(00966) "Cannot init worker");