fix: return HTTP 503 from /health when the service is down#298
Open
nangelovv wants to merge 1 commit into
Open
fix: return HTTP 503 from /health when the service is down#298nangelovv wants to merge 1 commit into
nangelovv wants to merge 1 commit into
Conversation
The /health handler used `return {"status": "DOWN"}, 503`, a Flask idiom.
FastAPI does not treat the tuple's second element as a status code: it
serializes the whole tuple as the response body and keeps the HTTP status at
200. Docker/Kubernetes/load-balancer liveness and readiness probes only look
at the status code, so they saw the service as healthy even when Postgres or
Atlas was unreachable.
Return a JSONResponse with an explicit 503 for both the unhealthy branch and
the exception branch. Add regression tests covering the UP (200), DOWN (503),
and exception (503) paths.
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.
Summary
/healthreturns HTTP 200 even when the health check fails, so liveness/readiness probes see a downed service as healthy. This returns an explicit503from the failure branches.Fixes #297
Cause
app/routes/document_routes.pyusedreturn {"status": "DOWN"}, 503, a Flask idiom. FastAPI serializes the returned tuple as the body ([{"status":"DOWN"},503]) and keeps the HTTP status at 200, so the 503 never reaches the client.Change
JSONResponse(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, content=...)for both the unhealthy branch and the exception branch.{"status": "UP"}→ 200).Tests
Added regression tests in
tests/test_main.py(monkeypatchingis_health_ok):test_health_check_up→ 200{"status": "UP"}test_health_check_down→ 503{"status": "DOWN"}test_health_check_exception→ 503 with error detailVerified live with Postgres stopped:
{"status":"UP"}{"status":"UP"}[{"status":"DOWN"},503]{"status":"DOWN"}tests/test_main.py: 13 passed.