Repository navigation
fix(serve): refuse a bad LAYA_LOG_LEVEL with a message, not a traceback - #1227
Merged
NandhaKishorM merged 1 commit intoOct 15, 2026
Merged
Conversation
laya-serve passed LAYA_LOG_LEVEL straight to uvicorn, which indexes its own LOG_LEVELS with .lower() and no .strip(), so a typo, a padded value from a .env, and a blank LAYA_LOG_LEVEL= all aborted with a bare KeyError naming the lowercased value instead of the variable. Resolve it in serve.py the way _resolve_port already resolves LAYA_PORT, against uvicorn's own LOG_LEVELS so the refusal cannot list a set uvicorn disagrees with, and read blank as unset like the rest of the LAYA_* family.
4 tasks done
Owner
|
Merged in 0.4.8. Reading the accepted names out of uvicorn's own The diagnosis is precise. uvicorn lowercases but does not strip, so a value copied out of a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
laya-servenow resolvesLAYA_LOG_LEVELinlaya/serve.pyinstead of handing the raw environmentvalue to uvicorn. A new
_resolve_log_level()sits next to_resolve_port(), strips and lowercasesthe value, treats blank as unset, and refuses a name uvicorn does not know with a
SystemExitthatlists the accepted spellings.
main()'slog_level=argument calls it.The accepted names are read from
uvicorn.config.LOG_LEVELSat the moment of validation, so therefusal cannot list a set uvicorn itself disagrees with.
Why
main()didlog_level=os.environ.get("LAYA_LOG_LEVEL", "info")and uvicorn looks that value upwith
LOG_LEVELS[self.log_level.lower()]— it lowercases, and nothing strips or validates. So everyone of these aborted the CLI with a bare
KeyErrorraised insideConfig.configure_logging, afterthe app is built:
Three things are wrong with that surface. It is a traceback, and the variable's own neighbour in the
same file states the opposite policy —
_resolve_port's docstring is "Exits with a message insteadof a traceback", and this is the only other raw env read on that
uvicorn.runcall. The exceptionnames neither the variable nor the caller, and it prints the lowercased value, so
' INFO 'reports
KeyError: 'info ', which looks like a uvicorn bug rather than a padded value in a.env.And the blank case is the family's:
LAYA_LOG_LEVEL=is what a declared-but-unfilled line in a.env, a Compose file, or a CI matrix writes, and nine of the elevenLAYA_*readers already answerthat with the documented default.
This also brings
LAYA_LOG_LEVELunder the same audit as the rest of the table: naming the defaultDEFAULT_LOG_LEVELat the read is the shapetests/test_env_docs.pyderives a documented defaultfrom, so a future change to it has to move
docs/http-api.md's row in the same PR (mutant M7 belowis exactly that edit, and it turns the audit red).
How it was verified
Three new tests in
tests/test_serve.py:test_resolve_log_level_refuses_a_bad_value_by_name— unset and the three blank spellings answerDEFAULT_LOG_LEVEL; each of uvicorn's own six names still works when written upper case andpadded; six bad spellings (
verbose,INFOO,notset,none,0,warn) raiseSystemExitwith the message compared in full, so the listed set is checked against
LOG_LEVELSrather thanagainst a copy.
test_a_bad_log_level_is_refused_before_uvicorn_sees_it— drivesmain()withuvicorn.runcaptured, the same seam
test_accepted_connections_set_tcp_nodelayuses:" WARNING "arrivesas
"warning",""arrives as the default, andverboseraises beforeuvicorn.runis reached(
captured == {}).test_uvicorn_dies_on_the_values_the_resolver_refuses— the harm witness, written against uvicornwith no
layacode on the path, pinning that the five spellings really do raiseKeyErrorandthat
info/warning/INFOconstruct fine. This is whatlaya-serveprinted before.The real CLI, run as
python -m laya.servewithLAYA_PRELOAD=0 HF_HUB_OFFLINE=1in a worktree atorigin/main(432312b) and in this branch,PYTHONPATH=.in both:LAYA_LOG_LEVELverboseKeyError: 'verbose'invalid LAYA_LOG_LEVEL 'verbose': must be one of critical, debug, error, info, trace, warning""KeyError: ''INFO: Uvicorn running on http://0.0.0.0:8123" "KeyError: ' 'info" WARNING "KeyError: ' warning 'warning, so the padded value reached uvicorn aswarningrather than falling backNo line in the diff exceeds 120 characters. Diff is 2 files, +105/-1, scanned for secrets: no match
for
api[_-]?key|secret|passw|credential|BEGIN .*PRIVATE|ghp_|xox[baprs]-|sk-[A-Za-z0-9]{10,}|AKIA|hf_[A-Za-z0-9]{20,}.Mutation check, each mutant applied to
laya/serve.py, the three new tests plus the whole file run,then
git checkout -- laya/serve.py:tracemain()reads the environment directly againDEFAULT_LOG_LEVELchanged to a value the table does not saytest_env_docs.pyfailsdefaults/docs/http-api.md's LAYA_LOG_LEVEL cell says what the code doesgit diff --quietafter the harness: clean, every file restored.Docs are unchanged on purpose:
docs/http-api.md:42anddocs/docker.md:347describe the value as"uvicorn log level", which is still exactly true — the accepted set is uvicorn's, not a new one — and
the documented default
infois what the code answers. Hunk-wise the branch is clear of the threeopen PRs that touch these files (#667, #687, #716):
laya/serve.pyis touched at 738-770 and 1564,tests/test_serve.pyat 27, 36 and 1645, and none of those three PRs has a hunk in those ranges.Checklist
main(branched from432312b;git merge-base HEAD origin/mainis432312b)tests/test_serve.py298 passed / 1 skipped, plus the suites listed above)and the default are what the pages already say