"""uwsgi-start.sh must carry the robustness flags in a coherent timeout ladder. The ladder fits under the edge load balancer: LB idle timeout > http-timeout > harakiri > longest downstream read. Each inner layer gives up before the one above it, so a stalled request is handled by the breaker (downstream read) or a worker kill (harakiri) before the LB drops the client. """ import re from pathlib import Path from core.hardening.resources import DOWNSTREAMS _UWSGI_START = Path(__file__).resolve().parents[3] / 'uwsgi-start.sh' # The edge load balancer idle timeout (terraform-fargate default for this service). The whole uWSGI # ladder must stay at or below it, or uWSGI would outlast the LB and leave a worker running past # the client's 504. If the infra value changes, update this and the uwsgi-start.sh flags together. _LB_IDLE_TIMEOUT_SECONDS = 60 def _script() -> str: return _UWSGI_START.read_text() def test_uwsgi_has_robustness_flags() -> None: """Both uwsgi invocations carry harakiri, worker-recycle, and graceful-drain flags.""" script = _script() for flag in ( '--http-timeout ', '--harakiri ', '--harakiri-verbose', '--max-requests ', '--max-requests-delta ', '--reload-mercy ', '--worker-reload-mercy ', '--die-on-term', ): # The script has two uwsgi invocations (ddtrace and non-ddtrace); both need the flag. assert script.count(flag) >= 2, ( f'{flag.strip()} missing from a uwsgi invocation' ) def test_timeout_ladder_is_consistent() -> None: """The ladder must hold: LB idle timeout >= http-timeout > harakiri > longest downstream read. Each layer outlasts the one below so it only fires after the lower layer's full budget, and the whole ladder stays at or under the load balancer so uWSGI never leaves a worker running past the client's 504. harakiri must not kill a worker before a legitimate downstream read (the slowest is Snowflake) completes. """ script = _script() http_timeouts = {int(v) for v in re.findall(r'--http-timeout\s+(\d+)', script)} harakiris = {int(v) for v in re.findall(r'--harakiri\s+(\d+)', script)} longest_read = max(policy.read_timeout for policy in DOWNSTREAMS.values()) assert harakiris, 'no "--harakiri " found' assert http_timeouts, 'no "--http-timeout " found' assert min(harakiris) > longest_read, ( f'harakiri {min(harakiris)} <= longest downstream read {longest_read}' ) assert min(http_timeouts) > max(harakiris), ( f'http-timeout {min(http_timeouts)} <= harakiri {max(harakiris)}' ) assert max(http_timeouts) <= _LB_IDLE_TIMEOUT_SECONDS, ( f'http-timeout {max(http_timeouts)} > LB idle timeout {_LB_IDLE_TIMEOUT_SECONDS}' )