Skip to content

mod_proxy: define run-time workers under proxy_mutex, and lock balancer walks - #1084

Merged
ylavic merged 7 commits into
apache:trunkfrom
ylavic:trunk-proxy-worker-mutex
Nov 25, 2026
Merged

ylavic merged 7 commits into
apache:trunkfrom
ylavic:trunk-proxy-worker-mutex

Conversation

@ylavic

@ylavic ylavic commented Nov 25, 2026

Copy link
Copy Markdown
Member

Workers added at run time (balancer-manager, mod_proxy_beacon) are allocated from the main server's configuration pool, shared by all the balancers of the server and of its vhosts, while the add holds only its own balancer's locks. Several readers also walk a balancer's workers array without the lock under which it grows. Concurrent adds can crash or hang children and garble worker names. This is the likely cause of the intermittent crashes in ap_proxy_sync_balancer() in the pytest CI's test_proxy_beacon_input.

Changes:

  • ap_proxy_define_worker_ex() takes proxy_mutex when it defines a balancer's worker once the MPM runs.
  • The balancer-manager (page, lookups, lbfactor), mod_status' proxy section and ProxyPassReverse walk a balancer's workers under its thread lock (PROXY_WORKERS_LOCK()). The page and the status hook walk a snapshot from ap_proxy_balancer_snap_workers(), a copy, so no lock is held across I/O.
  • Balancers that can't grow (no growth= in any of their vhost copies) never change their workers, so they're walked without the lock and keep trunk's locking unchanged.
  • The vhost copies of a growable balancer, which share its workers array, share one thread lock.
  • proxy_balancer gets an ungrowable bit (MMN minor bump).
  • New tests in test/modules/proxy/test_12_balancer.py (concurrent adds and walks, balancers without growth, BalancerPersist); test_proxy_beacon.py allows AH01171.

Testing: a reproducer with 16 balancers × 100 run-time adds fails 10 starts out of 10 without the changes (crashed children, wrong member lists, hangs), and 0 out of 10 with them. With the balancers in 16 vhosts, it fails 5 out of 5 without the changes and 0 out of 5 with them.

A worker added at run time (balancer-manager, mod_proxy_beacon through
balancer_manage()) is defined by balancer_add_worker() holding only that
balancer's global and thread locks, but ap_proxy_define_worker_ex() allocates
it from conf->pool, which the other balancers of the server share, as do the
vhosts (merge_proxy_config() gives them the main server's pool), and grows
the balancer's workers array from pconf.  ap_proxy_sync_balancer() and
ap_proxy_initialize_worker() allocate from those pools under proxy_mutex,
but an add to another balancer does not: concurrent adds and syncs corrupt
the pools, and a balancer's workers array or workers end up overwritten.
Concurrent balancer-manager adds to the balancers of several vhosts crash
children, hang them or garble the added workers' names, and this is the
likely cause of the intermittent crashes in ap_proxy_sync_balancer(), from
mod_proxy_beacon's reconcile, in the pytest CI's test_proxy_beacon_input.

Take proxy_mutex in ap_proxy_define_worker_ex() when it defines a worker of
a balancer in a child, the same way, after the balancer's locks (the order
balancer_add_worker() documents).  The workers defined in a child without a
balancer (mod_proxy's generic workers at child init, mod_proxy_hcheck's
workers in its own pool) don't need it.

* modules/proxy/proxy_util.c:
  (define_worker): Rename from ap_proxy_define_worker_ex().
  (ap_proxy_define_worker_ex): Call it under proxy_mutex for a balancer's
  worker at run time.
* modules/proxy/mod_proxy_balancer.c:
  (balancer_add_worker): Update the lock order comment.
* changes-entries/proxy-define-worker-mutex.txt: New entry.
…osts.

One client per vhost adds 100 workers to the vhost's balancer through its
balancer-manager, all at once, then each balancer must list exactly the
workers added to it, with no child crashed.  Without the previous commit
(run-time workers defined under proxy_mutex), the test failed 10 runs out of
10 (crashed children, reset connections, lost workers); with it, it passed
10 out of 10.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerAddWorkers): New test.
A balancer's workers array grows at run time, when the balancer-manager or
mod_proxy_beacon add a worker (balancer_add_worker()) and when a child syncs
the workers that other children added (ap_proxy_sync_balancer()), both under
the balancer's thread lock.  The request path walks the array under that lock,
but the balancer-manager's page, its worker lookups and lbfactor updates,
mod_proxy's status hook and the ProxyPassReverse mapping walk it without the
lock: they can read an array that moved, or a slot whose worker is not set
yet, and crash (balancer_display_page() while workers are added to other
balancers of the server).

Take the balancer's thread lock for these walks.  Those that write the
response (the balancer-manager's page, the status hook) walk a snapshot of
the array, ap_proxy_balancer_snap_workers(): a copy taken under the lock, so
that the lock is not held across I/O; the workers live as long as the
configuration, so the copy's pointers stay valid.

Workers can be added at run time only to a balancer with a growth: with
none, all of its worker slots hold configured workers, so the balancer-manager
refuses an add (no free slot) and a sync finds no worker to add.  The copies
of a balancer in the vhosts share its workers array (merge_balancers()), so
post_config marks ungrowable the copies of an array that none of them can
grow, and PROXY_WORKERS_LOCK() and PROXY_WORKERS_UNLOCK() skip the lock (and
the snapshot is the array itself) for them.

* modules/proxy/proxy_util.h:
  (PROXY_WORKERS_LOCK, PROXY_WORKERS_UNLOCK): New, the balancer's thread lock
  unless ungrowable.
  (ap_proxy_balancer_snap_workers): Declare.
* modules/proxy/proxy_util.c:
  (ap_proxy_balancer_snap_workers): New, the workers to walk.
  (ap_proxy_location_reverse_map): Walk a balancer's workers under its lock,
  unless ungrowable.
* modules/proxy/mod_proxy_balancer.c:
  (get_balancer_worker): New helper, look up a worker under the balancer's
  lock.
  (mark_ungrowable_balancers): New, mark the balancers that can't grow.
  (balancer_post_config): Call it.
  (recalc_factors): Take the balancer's lock.
  (balancer_manage, balancer_handler): Look up the worker with
  get_balancer_worker().
  (balancer_display_page): Walk snapshots of the workers arrays.
* modules/proxy/mod_proxy.c:
  (proxy_status_hook): Walk the snapshot of each balancer's workers.
* modules/proxy/mod_proxy.h:
  (proxy_balancer): Add ungrowable.
  (ap_proxy_get_worker_ex, ap_proxy_get_worker): Document the lock for a
  lookup in a balancer.
* include/ap_mmn.h:
  Minor bump for proxy_balancer's ungrowable.
…a balancer.

A balancer of the main server is copied into each vhost with its own proxy
configuration (merge_balancers()), and the copies share its workers array,
but balancer_child_init() gives each copy its own thread lock.  An add
through one vhost's balancer-manager, or a sync of the workers added through
that vhost by other children, grows the array under that copy's lock while
the request threads and the balancer-managers of the other vhosts walk it
under theirs: they can read an array that moved, or a worker that a sync has
pushed but not set up yet, and a failed add can pop a worker that another
copy pushed.

Give all the copies of a balancer that can grow the thread lock of the first
one initialized in the child, keyed by their workers array.  The copies of an
array that cannot grow (post_config marks them ungrowable) never change it, so
they keep a thread lock each, as before.  The copies keep their own global
locks, taken before the thread lock, so the lock order does not change.

* modules/proxy/mod_proxy_balancer.c:
  (balancer_child_init): Share the thread lock of the copies of a growable
  balancer.
* test/modules/proxy/test_12_balancer.py:
  (TestBalancerVhostCopies): New test.
The test polls /beacon right after the server starts, while the child
serving it has no member in the balancer yet: that request logs AH01171 ("No
workers in balancer") and the file's error log check fails when the file
runs first (e.g. alone), while it passes later in the full suite.

* test/pyhttpd/classic_errorlog.py:
  (PER_FILE): Allow AH01171 for test_proxy_beacon.py.
A balancer without a growth, and its vhost copies: the balancer-manager's
page, mod_status' proxy section and ProxyPassReverse list and map its
configured workers, and an add is refused.  A vhost copy without a growth
walks the workers while the copies of other vhosts, with a growth, add
workers to the array they share.  With BalancerPersist, a worker added with
a growth is replaced by a configured worker after a graceful or full restart
without the growth, and comes back after a restart with it.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerNoGrowth, TestBalancerMixedGrowth, TestBalancerPersist): New
  tests.
@ylavic

ylavic commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@jimjag this relates to mod_proxy_beacon and the backport proposal, which shouldn't go without this IMO.

@jimjag

jimjag commented Nov 25, 2026

Copy link
Copy Markdown
Member

Yann, one thought on ungrowable. It looks right to me as things stand, but it only holds because the pieces happen to line up (max_workers = nelts + growth, and the add path checking num_free_slots before it pushes). Nothing actually enforces it. If a later change lets ap_proxy_sync_balancer() push onto an ungrowable balancer, the unlocked walks are racy again and nobody notices.

I see two ways out: a cheap guard in sync (log and skip when b->ungrowable and the worker isn't found), or just drop it. pre_request already takes that lock on every request, so all it saves is an uncontended lock on the manager page, status and ProxyPassReverse. Dropping it also gets rid of the MMN bump, which makes the 2.4.x backport simpler.

Not a blocker, just curious what you think.

Cheers!

@ylavic

ylavic commented Nov 25, 2026 •

Copy link
Copy Markdown
Member Author

I added the skip on ->ungrowable so that balancers which can't add workers at run time keep current behaviour, with no added locking. That's every balancer without growth=, the default, and so most balancer configurations.

Whether to drop the conditional locking or add a guard in ap_proxy_sync_balancer() then depends on whether the walks it skips count as a fast path:

  • The balancer-manager page and mod_status' proxy section: I don't think they do.
  • ProxyPassReverse against a balancer://: that's per mapped response header, so mostly redirects, unless the backend sets e.g. Content-Location on every response. Even then, the walk under the lock is about as long as the lbmethod's walk pre_request already does under the same lock on every request.

If none of them counts as a fast path, I'd rather drop the bit: the walks always lock, the MMN bump goes, and the 2.4.x backport gets simpler. The vhost copies would still share the mutex only for growable balancers, worked out locally at child init, so balancers without growth= keep a mutex per copy on the request path, as today. If ProxyPassReverse should stay lock-free there, I'd keep the bit and add the guard in the sync (log and skip a worker that isn't found when b->ungrowable), so the invariant is enforced rather than implied.

What do you think?

@jimjag

jimjag commented Nov 25, 2026

Copy link
Copy Markdown
Member

LGTM!

@jimjag jimjag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ylavic

ylavic commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

@jimjag should I merge the PR as is, or add a ap_proxy_sync_balancer() guard for !ungrowable, or remove the conditional locking?
Nothing was changed yet from the initial PR, and my last message might have been ambiguous.

@jimjag

jimjag commented Nov 25, 2026

Copy link
Copy Markdown
Member

However you prefer is fine. I think the changes would be beneficial, but don't want to block the merge.

ylavic added a commit that referenced this pull request Nov 25, 2026
An ungrowable balancer's workers array is walked without its thread lock,
which holds only as long as nothing pushes onto the array at run time. Nothing
does today (an ungrowable balancer has no free worker slot), so make the sync
refuse, log and skip a worker it doesn't know, and the balancer-manager refuse
to add one, instead of relying on that.

* modules/proxy/proxy_util.c:
  (ap_proxy_sync_balancer): Don't add a worker to an ungrowable balancer.
* modules/proxy/mod_proxy_balancer.c:
  (balancer_add_worker): Likewise.
* modules/proxy/mod_proxy.h:
  (struct proxy_balancer): Say what enforces ungrowable.
@ylavic

ylavic commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Thanks Jim, I added the check/guard in ap_proxy_sync_balancer() and balancer_add_worker().

@ylavic
ylavic merged commit 557b62c into apache:trunk Nov 25, 2026
46 checks passed
jimjag pushed a commit to jimjag/httpd that referenced this pull request Nov 29, 2026
…er walks (apache#1084)

* mod_proxy: define run-time workers under proxy_mutex.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerAddWorkers): New test.

* mod_proxy: walk the workers of a balancer under its thread lock.

* modules/proxy/proxy_util.h:
  (PROXY_WORKERS_LOCK, PROXY_WORKERS_UNLOCK): New, the balancer's thread lock
  unless ungrowable.
  (ap_proxy_balancer_snap_workers): Declare.

* modules/proxy/proxy_util.c:
  (ap_proxy_balancer_snap_workers): New, the workers to walk.
  (ap_proxy_location_reverse_map): Walk a balancer's workers under its lock,
  unless ungrowable.

* modules/proxy/mod_proxy_balancer.c:
  (get_balancer_worker): New helper, look up a worker under the balancer's
  lock.
  (mark_ungrowable_balancers): New, mark the balancers that can't grow.
  (balancer_post_config): Call it.
  (recalc_factors): Take the balancer's lock.
  (balancer_manage, balancer_handler): Look up the worker with
  get_balancer_worker().
  (balancer_display_page): Walk snapshots of the workers arrays.

* modules/proxy/mod_proxy.c:
  (proxy_status_hook): Walk the snapshot of each balancer's workers.

* modules/proxy/mod_proxy.h:
  (proxy_balancer): Add ungrowable.
  (ap_proxy_get_worker_ex, ap_proxy_get_worker): Document the lock for a
  lookup in a balancer.

* include/ap_mmn.h:
  Minor bump for proxy_balancer's ungrowable.

* mod_proxy_balancer: share one thread lock across the vhost copies of a balancer.

* modules/proxy/mod_proxy_balancer.c:
  (balancer_child_init): Share the thread lock of the copies of a growable
  balancer.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerVhostCopies): New test.

* test: allow AH01171 in test_proxy_beacon.py.

* test/pyhttpd/classic_errorlog.py:
  (PER_FILE): Allow AH01171 for test_proxy_beacon.py.

* test: walk the workers of balancers that can't grow, and persisted ones.

* test/modules/proxy/test_12_balancer.py:
  (TestBalancerNoGrowth, TestBalancerMixedGrowth, TestBalancerPersist): New
  tests.

* mod_proxy: don't add a worker to an ungrowable balancer at run time.

* modules/proxy/proxy_util.c:
  (ap_proxy_sync_balancer): Don't add a worker to an ungrowable balancer.
* modules/proxy/mod_proxy_balancer.c:
  (balancer_add_worker): Likewise.
* modules/proxy/mod_proxy.h:
  (struct proxy_balancer): Say what enforces ungrowable.

(cherry picked from commit 557b62c)
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.

2 participants