Repository navigation
mod_proxy: define run-time workers under proxy_mutex, and lock balancer walks - #1084
Conversation
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.
|
@jimjag this relates to mod_proxy_beacon and the backport proposal, which shouldn't go without this IMO. |
|
Yann, one thought on I see two ways out: a cheap guard in sync (log and skip when Not a blocker, just curious what you think. Cheers! |
|
I added the skip on Whether to drop the conditional locking or add a guard in
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 What do you think? |
|
LGTM! |
|
@jimjag should I merge the PR as is, or add a ap_proxy_sync_balancer() guard for |
|
However you prefer is fine. I think the changes would be beneficial, but don't want to block the merge. |
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.
|
Thanks Jim, I added the check/guard in ap_proxy_sync_balancer() and balancer_add_worker(). |
…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)
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:
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.