Skip to content

mod_proxy: make proxy_mutex a thread mutex local to each child - #1103

Merged
ylavic merged 3 commits into
apache:trunkfrom
ylavic:trunk-proxy-mutex-thread
Nov 27, 2026
Merged

ylavic merged 3 commits into
apache:trunkfrom
ylavic:trunk-proxy-mutex-thread

Conversation

@ylavic

@ylavic ylavic commented Nov 27, 2026

Copy link
Copy Markdown
Member

proxy_mutex serializes the definition, initialization and sync of the
run-time workers (balancer-manager additions and, since #1084, every
run-time worker definition). What it protects is per-process data, each
child's own balancers and workers and the pools they are allocated from,
so a cross-process lock only costs system calls and a system resource
(a lock file, semaphore or shared memory, depending on the mechanism).

Changes:

  • proxy_mutex becomes a thread mutex, static in proxy_util.c, created in
    pconf at post_config so that each child inherits it unlocked; no child
    init anymore. Without APR_HAS_THREADS it's a no-op.
  • The "proxy" Mutex type stays registered, so existing Mutex ... proxy
    directives are still accepted; they have no effect anymore.
  • mod_proxy builds again without APR threads (it failed to compile, an
    undeclared rv since 2019, and "statement with no effect" errors from
    PROXY_THREAD_LOCK/UNLOCK() used as statements).
  • Lock order unchanged: balancer global lock, balancer thread lock, then
    proxy_mutex.
  • Docs: the Mutex directive's table lists the "proxy" type (no effect
    anymore) and mod_proxy_balancer's proxy-balancer-shm mutex, which was
    missing (it serializes run-time worker additions in shared memory).

This is meant as a small, backportable step (no API change). A further
trunk change could remove proxy_mutex entirely: give each balancer its
own pool (shared by its vhost copies) for its run-time workers, allocated
under the balancer's thread lock that the callers already hold, so that
no run-time allocation happens from a pool shared by all balancers
anymore. That would make the locking rule structural rather than
implicit, and allow removing workers at run time later (with per-worker
pools); it wouldn't change performance though, proxy_mutex being taken
only around run-time additions and first initializations.

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.
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.
* docs/manual/mod/core.xml (Mutex): Add the proxy-balancer-shm mutex of
  mod_proxy_balancer.
@ylavic
ylavic merged commit ced45d3 into apache:trunk Nov 27, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant