Conversation
…issing
Starlette's State.__getattr__ raises AttributeError when an attribute was
never set. catch_exception (the ASGI @app.exception_handler(Exception))
read request.state.trace_id / request.state.tenant_id directly, so when
a failure happened before the middleware that sets tenant_id (or even
trace_id) had a chance to run, the handler itself raised AttributeError.
That secondary failure discarded the intended logging.error(...) call and
prevented Starlette's ServerErrorMiddleware from re-raising the original
exception after the handler returns.
Use getattr(request.state, ..., None) for both attributes, and switch to
the module logger's .exception() (instead of bare logging.error) so the
original exception's traceback is preserved. The JSON 500 response shape
({message, trace_id, error_msg}) is unchanged.
Adds tests/test_catch_exception_handler.py covering tenant_id missing,
both missing, and both present.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
|
Contributor
|
Hey there and thank you for opening this pull request! 👋🏼 We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted. Details: |
Contributor
|
No linked issues found. Please add the corresponding issues in the pull request description. |
Member
|
seems like ai slop - no ticket, no reproduce, no evidence for user using Keep |
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.
Bug
The catch-all
@app.exception_handler(Exception)inkeep/api/api.py(catch_exception)reads
request.state.trace_idandrequest.state.tenant_iddirectly:Starlette's
State.__getattr__raisesAttributeErrorwhen an attribute was never set onrequest.state.tenant_idis only assigned byLoggingMiddleware, andtrace_idby anearlier middleware layer — so if the original exception happens before either of those runs,
the handler itself crashes with a secondary
AttributeError.That secondary crash is worse than a cosmetic bug: it means
logging.error(...)call for the original exception never completes, so theoriginal exception is not logged with a traceback (and, depending on log-pipeline routing,
may not be logged at all), and
ServerErrorMiddlewarenever gets its return value back — which is what it needs in orderto re-raise the original exception after the handler runs. The original failure is masked
by the handler's own
AttributeError.Fix
getattr(request.state, "...", None)instead of a direct attributeaccess, so a missing attribute degrades to
Nonerather than crashing the handler.logger.exception(...)instead of barelogging.error(...), sothe original exception's traceback is preserved (
logger.exceptionsuppliesexc_infoautomatically).
{message, trace_id, error_msg}) and status code are unchanged;trace_idmay now legitimately serialize asnullwhen it was never set, which is expected.Tests
Added
tests/test_catch_exception_handler.py, which calls the registered handler directly(
test_app.exception_handlers[Exception]) against a bareRequest/Statefor three cases:tenant_idmissing,trace_idpresentEach asserts HTTP 500, the expected JSON body (
message/trace_id/error_msg), and — thepoint of the fix — that no
AttributeErroris raised.I verified this red-first: with the old body restored, the "tenant_id missing" and "both
missing" cases fail with
AttributeError: 'State' object has no attribute '...'(the "bothpresent" case still passes unpatched, as expected, since both attributes exist in that case).
With the fix in place, all three pass.
This does not touch
#6089/#6100(differenttenantIdsurfaces) — it's scoped only tocatch_exception's handling ofrequest.state.