Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(serve): refuse a bad LAYA_LOG_LEVEL with a message, not a traceback

Aashish96287711bad4
diff --git a/laya/serve.py b/laya/serve.py
index c5eb2860..4d8c695c 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -738,6 +738,33 @@ def _resolve_port() -> int:
return port
+#: What `LAYA_LOG_LEVEL` answers when it is unset or blank: uvicorn's own default. Named so the read
+#: and the blank guard state it once -- a second copy is a second thing to keep in step.
+DEFAULT_LOG_LEVEL = "info"
+
+
+def _resolve_log_level() -> str:
+ """Log level from LAYA_LOG_LEVEL, validated. Exits with a message instead of a traceback.
+
+ The accepted names are uvicorn's own ``LOG_LEVELS``, not a list restated here, so the two cannot
+ drift. uvicorn lowercases the value but does not strip it, so both a typo and a value copied out
+ of a ``.env`` with padding reach ``LOG_LEVELS[...]`` and die inside ``Config.configure_logging``
+ with a bare ``KeyError`` naming the value -- a traceback that reads as a uvicorn bug, from the
+ one variable whose sibling in this file promises a message. A blank value is "not specified",
+ which is how the rest of the family reads it, and takes the documented default.
+ """
+ raw = os.environ.get("LAYA_LOG_LEVEL", DEFAULT_LOG_LEVEL)
+ level = raw.strip().lower()
+ if not level:
+ return DEFAULT_LOG_LEVEL
+ from uvicorn.config import LOG_LEVELS
+
+ if level not in LOG_LEVELS:
+ raise SystemExit("invalid LAYA_LOG_LEVEL %r: must be one of %s"
+ % (raw.strip(), ", ".join(sorted(LOG_LEVELS))))
+ return level
+
+
#: Values the walk in `_state_length_lower_bound_over` will examine before giving up. This bounds the
#: values *inspected*, not the total work: `stack.extend` pushes a container's elements without
#: charging them against the budget, so a 2 MiB body of many small values costs ~0.43 ms of walking
@@ -1537,7 +1564,7 @@ def main() -> None:
create_app(),
host=os.environ.get("LAYA_HOST", "0.0.0.0"),
port=_resolve_port(),
- log_level=os.environ.get("LAYA_LOG_LEVEL", "info"),
+ log_level=_resolve_log_level(),
http=NoDelayHTTPProtocol,
)
diff --git a/tests/test_serve.py b/tests/test_serve.py
index a2294d87..dde1b4b5 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -27,6 +27,7 @@ from laya.serve import ( # noqa: E402
BATCH_BODY_ITEM_CONTROLS,
BODY_CONTROLS,
BODY_REFUSALS,
+ DEFAULT_LOG_LEVEL,
DEFAULT_MAX_TOKEN_BUDGET,
MAX_BODY_BYTES,
MAX_STATE_CHARS,
@@ -35,6 +36,7 @@ from laya.serve import ( # noqa: E402
_check_request_limits,
_env_bool,
_LONE_SURROGATE_DETAIL,
+ _resolve_log_level,
_resolve_max_token_budget,
_resolve_idle_unload_seconds,
_resolve_max_loaded,
@@ -1643,6 +1645,81 @@ def test_resolve_max_token_budget_fallback(monkeypatch, caplog):
assert "invalid LAYA_MAX_TOKEN_BUDGET" in caplog.text
assert "LAYA_MAX_TOKEN_BUDGET must be positive" in caplog.text
+
+def test_resolve_log_level_refuses_a_bad_value_by_name(monkeypatch):
+ """`LAYA_LOG_LEVEL` answers with a message about the variable, never a traceback.
+
+ uvicorn looks the value up in its own `LOG_LEVELS` with `.lower()` and no `.strip()`, so a typo,
+ a padded value copied out of a `.env`, and a blank `LAYA_LOG_LEVEL=` all raise a bare `KeyError`
+ from inside `configure_logging` -- and the key it prints is the lowercased one, which names
+ neither the variable nor the caller. The accepted names are read from `LOG_LEVELS` itself rather
+ than restated here, so the refusal cannot list a set uvicorn disagrees with.
+ """
+ from uvicorn.config import LOG_LEVELS
+
+ monkeypatch.delenv("LAYA_LOG_LEVEL", raising=False)
+ assert _resolve_log_level() == DEFAULT_LOG_LEVEL
+ for blank in ("", " ", "\t"):
+ monkeypatch.setenv("LAYA_LOG_LEVEL", blank)
+ assert _resolve_log_level() == DEFAULT_LOG_LEVEL, repr(blank)
+
+ # Every spelling that worked before still works, case and padding included.
+ for name in sorted(LOG_LEVELS):
+ monkeypatch.setenv("LAYA_LOG_LEVEL", " %s " % name.upper())
+ assert _resolve_log_level() == name
+
+ for bad in ("verbose", "INFOO", "notset", "none", "0", "warn"):
+ monkeypatch.setenv("LAYA_LOG_LEVEL", bad)
+ with pytest.raises(SystemExit) as caught:
+ _resolve_log_level()
+ message = str(caught.value)
+ assert message == ("invalid LAYA_LOG_LEVEL %r: must be one of %s"
+ % (bad, ", ".join(sorted(LOG_LEVELS)))), message
+
+
+def test_a_bad_log_level_is_refused_before_uvicorn_sees_it(monkeypatch):
+ """`main()` is where the level is handed over, so that is where the promise is checked."""
+ import uvicorn
+
+ import laya.serve
+
+ captured = {}
+ monkeypatch.setattr(uvicorn, "run", lambda app, **kwargs: captured.update(kwargs))
+ monkeypatch.setattr(laya.serve, "create_app", lambda: None)
+
+ monkeypatch.setenv("LAYA_LOG_LEVEL", " WARNING ")
+ laya.serve.main()
+ assert captured["log_level"] == "warning"
+
+ monkeypatch.setenv("LAYA_LOG_LEVEL", "")
+ captured.clear()
+ laya.serve.main()
+ assert captured["log_level"] == DEFAULT_LOG_LEVEL
+
+ captured.clear()
+ monkeypatch.setenv("LAYA_LOG_LEVEL", "verbose")
+ with pytest.raises(SystemExit, match=r"invalid LAYA_LOG_LEVEL 'verbose'"):
+ laya.serve.main()
+ assert captured == {}, "the refusal must happen before uvicorn.run is reached"
+
+
+def test_uvicorn_dies_on_the_values_the_resolver_refuses():
+ """The witness that the guard guards a real crash: this is uvicorn's own behaviour, unmediated.
+
+ No `laya` code is on this path, which is the point -- `laya-serve` passed the environment
+ straight through, so every `KeyError` here is one the CLI printed.
+ """
+ import uvicorn
+
+ async def app():
+ return None
+
+ for value in ("", " ", "verbose", "notset", "INFO "):
+ with pytest.raises(KeyError):
+ uvicorn.Config(app, host="127.0.0.1", port=0, log_level=value)
+ for accepted in ("info", "warning", "INFO"):
+ uvicorn.Config(app, host="127.0.0.1", port=0, log_level=accepted)
+
BATCH_REQ = {
"states": ["first state", "second state"],
"questions": REQ["questions"],