From 2c43767bbe5573db5df158cb6f0023a53d8ecb1c Mon Sep 17 00:00:00 2001 From: "Grapette.L" <90967079+Lesereingrape@users.noreply.github.com> Date: Thu, 24 Sep 2026 15:56:38 +0800 Subject: [PATCH] fix(gateway): treat a blank GATEWAY_HOST/GATEWAY_PORT as unset (#5801) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit os.getenv's default only covers an unset variable, so `docker run -e GATEWAY_PORT=` — or `GATEWAY_PORT=${PORT}` with PORT unset — reached int("") and aborted the create_app() import that both entry points run. Blank now falls back to the declared default, while "0" stays a real port. --- backend/app/gateway/config.py | 5 +++-- backend/tests/test_gateway_docs_toggle.py | 25 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/backend/app/gateway/config.py b/backend/app/gateway/config.py index 06a7d5b1ac..7f9bb02c25 100644 --- a/backend/app/gateway/config.py +++ b/backend/app/gateway/config.py @@ -19,8 +19,9 @@ def get_gateway_config() -> GatewayConfig: global _gateway_config if _gateway_config is None: _gateway_config = GatewayConfig( - host=os.getenv("GATEWAY_HOST", "0.0.0.0"), - port=int(os.getenv("GATEWAY_PORT", "8001")), + # A blank means "unset" (`-e GATEWAY_PORT=`, or `${PORT}` with PORT unset); int("") would abort the create_app() import + host=os.getenv("GATEWAY_HOST") or "0.0.0.0", + port=int(os.getenv("GATEWAY_PORT") or "8001"), enable_docs=os.getenv("GATEWAY_ENABLE_DOCS", "true").lower() == "true", ) return _gateway_config diff --git a/backend/tests/test_gateway_docs_toggle.py b/backend/tests/test_gateway_docs_toggle.py index 986370286a..24a7c7d02b 100644 --- a/backend/tests/test_gateway_docs_toggle.py +++ b/backend/tests/test_gateway_docs_toggle.py @@ -78,6 +78,31 @@ def test_enable_docs_unexpected_value_disables(): assert config.enable_docs is False, f"Expected False for GATEWAY_ENABLE_DOCS={value}" +def test_blank_host_and_port_fall_back_to_defaults(): + """A blank GATEWAY_HOST/GATEWAY_PORT means unset, not a crash or an empty bind address. + + ``docker run -e GATEWAY_PORT=`` and ``environment: [GATEWAY_PORT=${PORT}]`` with PORT + unset both reach us as an empty string, and create_app() reads this at import time. + """ + with patch.dict(os.environ, {"GATEWAY_HOST": "", "GATEWAY_PORT": ""}): + _reset_gateway_config() + from app.gateway.config import get_gateway_config + + config = get_gateway_config() + assert config.port == 8001 + assert config.host == "0.0.0.0" + + +def test_zero_port_is_not_treated_as_unset(): + """GATEWAY_PORT=0 asks the OS for a free port, so it must survive the blank fallback.""" + with patch.dict(os.environ, {"GATEWAY_PORT": "0"}): + _reset_gateway_config() + from app.gateway.config import get_gateway_config + + config = get_gateway_config() + assert config.port == 0 + + # --------------------------------------------------------------------------- # App-level endpoint visibility # ---------------------------------------------------------------------------