From 69a0dfa9b924665cd65f76085b2c9d3476e222f8 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 30 Oct 2026 14:53:54 +0100 Subject: [PATCH] fix(sys-svc-webserver-core): size proxy workers from the proxy alone The shared OpenResty nginx.conf took worker_processes and worker_connections from WEBSERVER_WORKER_*, which are the min() of svc-prx-openresty's limits and the current play's application_id. The file renders once, in the scope of whichever role loads the proxy first, so that role's limits capped every vhost: on feature/store-i18n web-svc-mirror (pids_limit 64) loaded it first, every vhost got worker_connections 64, and openresty raised 555 "64 worker_connections are not enough while connecting to upstream" alerts while a single openwebui page fires ~150-170 chunk requests (CI run 40979252521, homeassistant#1). Main carries the same wiring. Add WEBSERVER_PROXY_WORKER_PROCESSES and WEBSERVER_PROXY_WORKER_CONNECTIONS, computed from svc-prx-openresty's own openresty service only (same resource_filter fallbacks, at least one worker), and render nginx.conf from them. WEBSERVER_WORKER_* keep pairing the proxy with the play for app-internal nginx such as Nextcloud's. The values can only stay equal or rise. Found by the i18n session's triage of homeassistant#1. Co-Authored-By: Claude Opus 5.5 --- group_vars/all/04_webserver.yml | 12 ++ .../templates/nginx.conf.j2 | 4 +- .../test_webserver_worker_limits.py | 146 ++++++++++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 tests/unit/python/group_vars/test_webserver_worker_limits.py diff --git a/group_vars/all/04_webserver.yml b/group_vars/all/04_webserver.yml index 5168689cb9..45f467e50c 100644 --- a/group_vars/all/04_webserver.yml +++ b/group_vars/all/04_webserver.yml @@ -25,6 +25,18 @@ WEBSERVER_WORKER_CONNECTIONS: >- ] | min }} +# Not WEBSERVER_WORKER_*: their application_id is whichever role loaded the shared proxy first, whose limits then cap every vhost. +WEBSERVER_PROXY_WORKER_PROCESSES: >- + {{ + [ + (lookup('applications') | resource_filter('svc-prx-openresty', 'cpus', '', RESOURCE_CPUS)) | float | int, + 1 + ] | max + }} + +WEBSERVER_PROXY_WORKER_CONNECTIONS: >- + {{ lookup('applications') | resource_filter('svc-prx-openresty', 'pids_limit', '', RESOURCE_PIDS_LIMIT) | int }} + # Response security headers of every vhost; an app overrides any key under `security_headers` in its meta/server.yml. WEBSERVER_SECURITY_HEADERS: nosniff: false # X-Content-Type-Options: nosniff diff --git a/roles/sys-svc-webserver-core/templates/nginx.conf.j2 b/roles/sys-svc-webserver-core/templates/nginx.conf.j2 index e8cfe19861..97b3e84dbf 100644 --- a/roles/sys-svc-webserver-core/templates/nginx.conf.j2 +++ b/roles/sys-svc-webserver-core/templates/nginx.conf.j2 @@ -1,7 +1,7 @@ -worker_processes {{ WEBSERVER_WORKER_PROCESSES }}; +worker_processes {{ WEBSERVER_PROXY_WORKER_PROCESSES }}; events { - worker_connections {{ WEBSERVER_WORKER_CONNECTIONS }}; + worker_connections {{ WEBSERVER_PROXY_WORKER_CONNECTIONS }}; } http { diff --git a/tests/unit/python/group_vars/test_webserver_worker_limits.py b/tests/unit/python/group_vars/test_webserver_worker_limits.py new file mode 100644 index 0000000000..16a9997311 --- /dev/null +++ b/tests/unit/python/group_vars/test_webserver_worker_limits.py @@ -0,0 +1,146 @@ +"""The shared proxy's nginx.conf takes its worker limits from the proxy alone. + +``nginx.conf`` of ``sys-svc-webserver-core`` renders once, in the scope of +whichever role loads ``svc-prx-openresty`` first. ``WEBSERVER_WORKER_*`` pair the +proxy with that role's ``application_id``, so a 64-pid role capped every vhost at +64 worker_connections. +""" + +from __future__ import annotations + +import unittest +from pathlib import Path +from typing import Any, ClassVar + +from ansible.parsing.dataloader import DataLoader +from ansible.template import Templar, trust_as_template + +from utils.cache import _reset_cache_for_tests +from utils.cache.carrier import ( + APPLICATIONS_RENDERED_FACT, + merged_applications_cache_key, +) +from utils.cache.files import PROJECT_ROOT, read_text +from utils.cache.yaml import load_yaml + +_WEBSERVER_VARS = Path(PROJECT_ROOT) / "group_vars" / "all" / "04_webserver.yml" +_NGINX_CONF = ( + Path(PROJECT_ROOT) + / "roles" + / "sys-svc-webserver-core" + / "templates" + / "nginx.conf.j2" +) + +_FIRST_LOADER = "web-svc-mirror" + + +def _applications(proxy_cpus: float) -> dict[str, Any]: + """Return a proxy with 256 pids and a first-loading role with 64 pids, 1 cpu. + + Args: + proxy_cpus: the proxy's ``cpus``. + """ + return { + "svc-prx-openresty": { + "services": {"openresty": {"cpus": proxy_cpus, "pids_limit": 256}} + }, + _FIRST_LOADER: {"services": {"mirror": {"cpus": 1, "pids_limit": 64}}}, + } + + +class TestWebserverWorkerLimits(unittest.TestCase): + group_vars: ClassVar[dict[str, Any]] + + @classmethod + def setUpClass(cls) -> None: + cls.group_vars = load_yaml(_WEBSERVER_VARS) + + def setUp(self) -> None: + _reset_cache_for_tests() + self.addCleanup(_reset_cache_for_tests) + + def _render( + self, expression: str, applications: dict[str, Any], **scope: Any + ) -> str: + """Render *expression* through Ansible's Templar over the group_vars. + + Args: + expression: Jinja source, e.g. ``"{{ WEBSERVER_WORKER_CONNECTIONS }}"``. + applications: payload ``lookup('applications')`` serves. + scope: play variables added on top of ``application_id``. + + Returns: + The rendered text, stripped. + """ + variables: dict[str, Any] = { + key: trust_as_template(value) if isinstance(value, str) else value + for key, value in self.group_vars.items() + } + variables.update( + application_id=_FIRST_LOADER, + RESOURCE_CPUS="0.5", + RESOURCE_PIDS_LIMIT=512, + **scope, + ) + key = merged_applications_cache_key(variables) + variables[APPLICATIONS_RENDERED_FACT] = { + "key": [key[0], list(key[1])], + "applications": applications, + } + templar = Templar(loader=DataLoader(), variables=variables) + return str(templar.template(trust_as_template(expression))).strip() + + def test_proxy_connections_follow_the_proxy_alone(self) -> None: + for scope in ({}, {"service_name": "mirror"}): + with self.subTest(**scope): + self.assertEqual( + self._render( + "{{ WEBSERVER_PROXY_WORKER_CONNECTIONS }}", + _applications(4), + **scope, + ), + "256", + ) + + def test_proxy_processes_follow_the_proxy_alone(self) -> None: + for scope in ({}, {"service_name": "mirror"}): + with self.subTest(**scope): + self.assertEqual( + self._render( + "{{ WEBSERVER_PROXY_WORKER_PROCESSES }}", + _applications(4), + **scope, + ), + "4", + ) + + def test_a_sub_cpu_proxy_still_runs_one_worker(self) -> None: + self.assertEqual( + self._render("{{ WEBSERVER_PROXY_WORKER_PROCESSES }}", _applications(0.5)), + "1", + ) + + def test_app_internal_limits_still_pair_with_the_play(self) -> None: + applications = _applications(4) + self.assertEqual( + self._render("{{ WEBSERVER_WORKER_CONNECTIONS }}", applications), "64" + ) + self.assertEqual( + self._render("{{ WEBSERVER_WORKER_PROCESSES }}", applications), "1" + ) + + def test_shared_proxy_nginx_conf_uses_the_proxy_limits(self) -> None: + worker_lines = [ + line.strip() + for line in read_text(str(_NGINX_CONF)).splitlines() + if line.strip().startswith("worker_") + ] + rendered = self._render("\n".join(worker_lines), _applications(4)) + self.assertEqual( + rendered.splitlines(), ["worker_processes 4;", "worker_connections 256;"] + ) + + +if __name__ == "__main__": + unittest.main()