From 81e31e678c2d3b696031847c98bba48a2650579e Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 27 Nov 2026 14:55:42 +0100 Subject: [PATCH 1/3] mod_proxy: make proxy_mutex a thread mutex local to each child. proxy_mutex serializes the definition, initialization and sync of the run-time workers. Those are per-process data, allocated from per-process pools, so a cross-process lock is not needed. Create it as a thread mutex in pconf at post_config, for each child to inherit unlocked, and no longer init it in child_init. The "proxy" Mutex type stays registered so that existing Mutex directives naming it are still accepted; they have no effect. * modules/proxy/mod_proxy.c (proxy_mutex): Move to proxy_util.c. (proxy_post_config): Create it with proxy_util_post_config(). (child_init): Drop apr_global_mutex_child_init(). (proxy_pre_config): Say why the "proxy" type is still registered. * modules/proxy/proxy_util.c (proxy_mutex): A static thread mutex, with APR_HAS_THREADS only. (proxy_util_post_config): New, creates it. (proxy_mutex_lock, proxy_mutex_unlock): New helpers, no-ops without APR_HAS_THREADS. (ap_proxy_define_worker_ex, ap_proxy_initialize_worker, ap_proxy_sync_balancer): Use them. * modules/proxy/proxy_util.h (proxy_util_post_config): New internal declaration. * docs/manual/mod/core.xml (Mutex): List the "proxy" mutex, without effect. * changes-entries/proxy-mutex-thread.txt: New entry. --- changes-entries/proxy-mutex-thread.txt | 4 ++ docs/manual/mod/core.xml | 7 ++++ modules/proxy/mod_proxy.c | 27 +++++-------- modules/proxy/proxy_util.c | 55 +++++++++++++++++++++----- modules/proxy/proxy_util.h | 9 +++++ 5 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 changes-entries/proxy-mutex-thread.txt 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..a2e79f0c660 100644 --- a/docs/manual/mod/core.xml +++ b/docs/manual/mod/core.xml @@ -4000,6 +4000,13 @@ 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) + 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/proxy_util.c b/modules/proxy/proxy_util.c index 3d4d2434dbb..2b0f96add95 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 */ @@ -2259,9 +2295,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 +2444,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 +2457,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 +2467,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 +2504,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 +4583,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..9eaa3c46bbd 100644 --- a/modules/proxy/proxy_util.h +++ b/modules/proxy/proxy_util.h @@ -64,6 +64,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 * From c1c9ab52f432f7714cf354156461f6f3f941b1b7 Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 27 Nov 2026 14:55:42 +0100 Subject: [PATCH 2/3] mod_proxy: fix the build without threads. Without APR_HAS_THREADS, PROXY_THREAD_LOCK/UNLOCK() (and so PROXY_WORKERS_LOCK/UNLOCK()) expanded to a constant, which used as a statement is a "statement with no effect", and some status variables were undeclared or unused. * modules/proxy/mod_proxy.h (PROXY_THREAD_LOCK, PROXY_THREAD_UNLOCK): Call the internal inline ap_proxy__nolock() returning APR_SUCCESS without threads. * modules/proxy/proxy_util.h (PROXY_WORKERS_LOCK, PROXY_WORKERS_UNLOCK): Same as PROXY_THREAD_LOCK/UNLOCK() without threads. * modules/proxy/proxy_util.c (ap_proxy_initialize_balancer): Declare rv with or without threads. * modules/proxy/mod_proxy_balancer.c (find_best_worker, proxy_balancer_pre_request, proxy_balancer_post_request): Declare rv with threads only. --- modules/proxy/mod_proxy.h | 12 ++++++++++-- modules/proxy/mod_proxy_balancer.c | 6 ++++++ modules/proxy/proxy_util.c | 2 -- modules/proxy/proxy_util.h | 5 +++++ 4 files changed, 21 insertions(+), 4 deletions(-) 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 2b0f96add95..b6f04404231 100644 --- a/modules/proxy/proxy_util.c +++ b/modules/proxy/proxy_util.c @@ -1382,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; diff --git a/modules/proxy/proxy_util.h b/modules/proxy/proxy_util.h index 9eaa3c46bbd..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 From 0db1186abd391d5dcb8215ba2926cc74cc2dc4f3 Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 27 Nov 2026 14:55:42 +0100 Subject: [PATCH 3/3] docs: list the proxy-balancer-shm mutex in the Mutex directive. * docs/manual/mod/core.xml (Mutex): Add the proxy-balancer-shm mutex of mod_proxy_balancer. --- docs/manual/mod/core.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/manual/mod/core.xml b/docs/manual/mod/core.xml index a2e79f0c660..54b352ccb0e 100644 --- a/docs/manual/mod/core.xml +++ b/docs/manual/mod/core.xml @@ -4007,6 +4007,12 @@ or specified mutexes 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