diff --git a/changes-entries/proxy-mutex-thread.txt b/changes-entries/proxy-mutex-thread.txt
new file mode 100644
index 00000000000..ee143dc9e07
--- /dev/null
+++ b/changes-entries/proxy-mutex-thread.txt
@@ -0,0 +1,4 @@
+ *) mod_proxy: Use a mutex local to each child process to protect the
+ run-time workers, instead of a cross-process one. The "proxy" type
+ of the Mutex directive is still accepted but has no effect anymore.
+ [Yann Ylavic]
diff --git a/docs/manual/mod/core.xml b/docs/manual/mod/core.xml
index 2157742adf7..54b352ccb0e 100644
--- a/docs/manual/mod/core.xml
+++ b/docs/manual/mod/core.xml
@@ -4000,6 +4000,19 @@ or specified mutexes
mod_ldap |
LDAP result cache |
+
+ proxy |
+ mod_proxy |
+ none: accepted for compatibility, it has no effect (the
+ run-time workers are protected by a mutex local to each child
+ process) |
+
+
+ proxy-balancer-shm |
+ mod_proxy_balancer |
+ workers added to a balancer at run time, in shared memory
+ (balancer-manager) |
+
rewrite-map |
mod_rewrite |
diff --git a/modules/proxy/mod_proxy.c b/modules/proxy/mod_proxy.c
index d9e7de0383e..a8cf988e688 100644
--- a/modules/proxy/mod_proxy.c
+++ b/modules/proxy/mod_proxy.c
@@ -76,7 +76,6 @@ proxy_wstat_t PROXY_DECLARE_DATA proxy_wstat_tbl[] = {
};
static const char * const proxy_id = "proxy";
-apr_global_mutex_t *proxy_mutex = NULL;
/*
* A Web proxy module. Stages:
@@ -3159,12 +3158,11 @@ static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *main_s)
{
server_rec *s = main_s;
- apr_status_t rv = ap_global_mutex_create(&proxy_mutex, NULL,
- proxy_id, NULL, s, pconf, 0);
- if (rv != APR_SUCCESS) {
- ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02478)
- "failed to create %s mutex", proxy_id);
- return rv;
+ int rc;
+
+ rc = proxy_util_post_config(pconf, plog, ptemp, main_s);
+ if (rc != OK) {
+ return rc;
}
proxy_ssl_enable = APR_RETRIEVE_OPTIONAL_FN(ssl_proxy_enable);
@@ -3174,7 +3172,7 @@ static int proxy_post_config(apr_pool_t *pconf, apr_pool_t *plog,
ap_proxy_strmatch_domain = apr_strmatch_precompile(pconf, "domain=", 0);
for (; s; s = s->next) {
- int rc, i;
+ int i;
proxy_server_conf *sconf =
ap_get_module_config(s->module_config, &proxy_module);
ap_conf_vector_t **sections =
@@ -3326,15 +3324,6 @@ static void child_init(apr_pool_t *p, server_rec *s)
proxy_worker *forward = NULL;
proxy_worker *reverse = NULL;
- apr_status_t rv = apr_global_mutex_child_init(&proxy_mutex,
- apr_global_mutex_lockfile(proxy_mutex),
- p);
- if (rv != APR_SUCCESS) {
- ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(02479)
- "could not init proxy_mutex in child");
- exit(1); /* Ugly, but what else? */
- }
-
main_conf = ap_get_module_config(s->module_config, &proxy_module);
for (; s; s = s->next) {
void *sconf = s->module_config;
@@ -3407,6 +3396,10 @@ static void child_init(apr_pool_t *p, server_rec *s)
static int proxy_pre_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptemp)
{
+ /* The "proxy" mutex is a thread mutex now (see proxy_util.c), but the
+ * type stays registered so that existing Mutex directives naming it are
+ * still accepted; they have no effect.
+ */
apr_status_t rv = ap_mutex_register(pconf, proxy_id, NULL,
APR_LOCK_DEFAULT, 0);
if (rv != APR_SUCCESS) {
diff --git a/modules/proxy/mod_proxy.h b/modules/proxy/mod_proxy.h
index 7dcb875e4ad..a15bb7d52a1 100644
--- a/modules/proxy/mod_proxy.h
+++ b/modules/proxy/mod_proxy.h
@@ -604,8 +604,16 @@ struct proxy_balancer_method {
#define PROXY_THREAD_LOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_lock((x)->tmutex) : APR_SUCCESS)
#define PROXY_THREAD_UNLOCK(x) ( (x) && (x)->tmutex ? apr_thread_mutex_unlock((x)->tmutex) : APR_SUCCESS)
#else
-#define PROXY_THREAD_LOCK(x) (APR_SUCCESS)
-#define PROXY_THREAD_UNLOCK(x) (APR_SUCCESS)
+/* Without threads, a function call rather than a constant so that the macros
+ * can be used as statements without "statement with no effect" warnings
+ * (internal to these macros, not API).
+ */
+static APR_INLINE apr_status_t ap_proxy__nolock(void)
+{
+ return APR_SUCCESS;
+}
+#define PROXY_THREAD_LOCK(x) ((void)(x), ap_proxy__nolock())
+#define PROXY_THREAD_UNLOCK(x) ((void)(x), ap_proxy__nolock())
#endif
#define PROXY_GLOBAL_LOCK(x) ( (x) && (x)->gmutex ? apr_global_mutex_lock((x)->gmutex) : APR_SUCCESS)
diff --git a/modules/proxy/mod_proxy_balancer.c b/modules/proxy/mod_proxy_balancer.c
index 2f93c5c8ed2..5d706b6adc9 100644
--- a/modules/proxy/mod_proxy_balancer.c
+++ b/modules/proxy/mod_proxy_balancer.c
@@ -339,7 +339,9 @@ static proxy_worker *find_best_worker(proxy_balancer *balancer,
request_rec *r)
{
proxy_worker *candidate = NULL;
+#if APR_HAS_THREADS
apr_status_t rv;
+#endif
#if APR_HAS_THREADS
if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
@@ -466,7 +468,9 @@ static int proxy_balancer_pre_request(proxy_worker **worker,
proxy_worker *runtime;
char *route = NULL;
const char *sticky = NULL;
+#if APR_HAS_THREADS
apr_status_t rv;
+#endif
*worker = NULL;
/* Step 1: check if the url is for us
@@ -640,7 +644,9 @@ static int proxy_balancer_post_request(proxy_worker *worker,
proxy_server_conf *conf)
{
+#if APR_HAS_THREADS
apr_status_t rv;
+#endif
#if APR_HAS_THREADS
if ((rv = PROXY_THREAD_LOCK(balancer)) != APR_SUCCESS) {
diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c
index 3d4d2434dbb..b6f04404231 100644
--- a/modules/proxy/proxy_util.c
+++ b/modules/proxy/proxy_util.c
@@ -73,7 +73,43 @@ static int lb_workers_limit = 0;
const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_path;
const apr_strmatch_pattern PROXY_DECLARE_DATA *ap_proxy_strmatch_domain;
-extern apr_global_mutex_t *proxy_mutex;
+#if APR_HAS_THREADS
+/* Serializes, within a process, the definition, initialization and sync of
+ * the run-time workers. Those are per-process data (the workers and
+ * balancers, and the pools they are allocated from), so it is a thread
+ * mutex, created at post_config for each child to inherit unlocked.
+ */
+static apr_thread_mutex_t *proxy_mutex = NULL;
+
+static APR_INLINE void proxy_mutex_lock(void)
+{
+ apr_thread_mutex_lock(proxy_mutex);
+}
+
+static APR_INLINE void proxy_mutex_unlock(void)
+{
+ apr_thread_mutex_unlock(proxy_mutex);
+}
+#else
+#define proxy_mutex_lock() ((void)0)
+#define proxy_mutex_unlock() ((void)0)
+#endif
+
+int proxy_util_post_config(apr_pool_t *pconf, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *main_s)
+{
+#if APR_HAS_THREADS
+ apr_status_t rv = apr_thread_mutex_create(&proxy_mutex,
+ APR_THREAD_MUTEX_DEFAULT,
+ pconf);
+ if (rv != APR_SUCCESS) {
+ ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02478)
+ "failed to create proxy mutex");
+ return HTTP_INTERNAL_SERVER_ERROR;
+ }
+#endif
+ return OK;
+}
static const apr_time_t *proxy_start_time; /* epoch for expiring addresses */
@@ -1346,9 +1382,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_share_balancer(proxy_balancer *balancer,
PROXY_DECLARE(apr_status_t) ap_proxy_initialize_balancer(proxy_balancer *balancer, server_rec *s, apr_pool_t *p)
{
-#if APR_HAS_THREADS
apr_status_t rv = APR_SUCCESS;
-#endif
ap_slotmem_provider_t *storage = balancer->storage;
apr_size_t size;
unsigned int num;
@@ -2259,9 +2293,9 @@ PROXY_DECLARE(char *) ap_proxy_define_worker_ex(apr_pool_t *p,
if (!balancer || ap_state_query(AP_SQ_MAIN_STATE) != AP_SQ_MS_RUN_MPM) {
return define_worker(p, worker, balancer, conf, url, mask);
}
- apr_global_mutex_lock(proxy_mutex);
+ proxy_mutex_lock();
err = define_worker(p, worker, balancer, conf, url, mask);
- apr_global_mutex_unlock(proxy_mutex);
+ proxy_mutex_unlock();
return err;
}
@@ -2408,7 +2442,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
ap_proxy_worker_get_name(worker));
}
else {
- apr_global_mutex_lock(proxy_mutex);
+ proxy_mutex_lock();
/* Check again after we got the lock if we are still uninitialized */
if (!(AP_VOLATILIZE_T(unsigned int, worker->local_status) & PROXY_WORKER_INITIALIZED)) {
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00927)
@@ -2421,7 +2455,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(00928)
"can not create worker thread mutex");
- apr_global_mutex_unlock(proxy_mutex);
+ proxy_mutex_unlock();
return rv;
}
}
@@ -2431,7 +2465,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
if (worker->cp == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00929)
"can not create connection pool");
- apr_global_mutex_unlock(proxy_mutex);
+ proxy_mutex_unlock();
return APR_EGENERAL;
}
@@ -2468,7 +2502,7 @@ PROXY_DECLARE(apr_status_t) ap_proxy_initialize_worker(proxy_worker *worker, ser
worker->local_status |= (PROXY_WORKER_INITIALIZED);
}
}
- apr_global_mutex_unlock(proxy_mutex);
+ proxy_mutex_unlock();
}
if (rv == APR_SUCCESS) {
@@ -4547,11 +4581,10 @@ PROXY_DECLARE(apr_status_t) ap_proxy_sync_balancer(proxy_balancer *b, server_rec
"that can't grow", b->s->name, index, shm->name);
continue;
}
- /* XXX: a thread mutex is maybe enough here */
- apr_global_mutex_lock(proxy_mutex);
+ proxy_mutex_lock();
runtime = apr_array_push(b->workers);
*runtime = apr_pcalloc(conf->pool, sizeof(proxy_worker));
- apr_global_mutex_unlock(proxy_mutex);
+ proxy_mutex_unlock();
(*runtime)->hash = shm->hash;
(*runtime)->balancer = b;
(*runtime)->s = shm;
diff --git a/modules/proxy/proxy_util.h b/modules/proxy/proxy_util.h
index 599ea89bb7f..8b7419a2130 100644
--- a/modules/proxy/proxy_util.h
+++ b/modules/proxy/proxy_util.h
@@ -37,10 +37,15 @@ PROXY_DECLARE(int) ap_proxy_is_word(struct dirconn_entry *This, apr_pool_t *p);
* walk it holding that lock. The array of an ungrowable balancer never
* changes, and is walked without it.
*/
+#if APR_HAS_THREADS
#define PROXY_WORKERS_LOCK(b) ((b)->ungrowable ? APR_SUCCESS : \
PROXY_THREAD_LOCK(b))
#define PROXY_WORKERS_UNLOCK(b) ((b)->ungrowable ? APR_SUCCESS : \
PROXY_THREAD_UNLOCK(b))
+#else
+#define PROXY_WORKERS_LOCK(b) PROXY_THREAD_LOCK(b)
+#define PROXY_WORKERS_UNLOCK(b) PROXY_THREAD_UNLOCK(b)
+#endif
/**
* The workers of a balancer to walk, for a walk that does not hold the
@@ -64,6 +69,15 @@ extern PROXY_DECLARE_DATA const apr_strmatch_pattern *ap_proxy_strmatch_domain;
*/
void proxy_util_register_hooks(apr_pool_t *p);
+/**
+ * Post-config initialization of what proxy_util.c owns (e.g. the mutex
+ * serializing the definition, initialization and sync of the run-time
+ * workers in each process), called from mod_proxy's post_config hook with
+ * the same arguments.
+ */
+int proxy_util_post_config(apr_pool_t *pconf, apr_pool_t *plog,
+ apr_pool_t *ptemp, server_rec *main_s);
+
/*
* Get the busy counter from the shared worker memory
*