Skip to content

fix: return HTTP 503 from /health when the service is down#298

Open
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/health-check-status-code
Open

fix: return HTTP 503 from /health when the service is down#298
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/health-check-status-code

Conversation

@nangelovv

Copy link
Copy Markdown

Summary

/health returns HTTP 200 even when the health check fails, so liveness/readiness probes see a downed service as healthy. This returns an explicit 503 from the failure branches.

Fixes #297

Cause

app/routes/document_routes.py used return {"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

  • Return JSONResponse(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, content=...) for both the unhealthy branch and the exception branch.
  • The UP branch is unchanged ({"status": "UP"} → 200).

Tests

Added regression tests in tests/test_main.py (monkeypatching is_health_ok):

  • test_health_check_up → 200 {"status": "UP"}
  • test_health_check_down → 503 {"status": "DOWN"}
  • test_health_check_exception → 503 with error detail

Verified live with Postgres stopped:

State Before After
DB up 200 {"status":"UP"} 200 {"status":"UP"}
DB down 200 [{"status":"DOWN"},503] 503 {"status":"DOWN"}

tests/test_main.py: 13 passed.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/health returns HTTP 200 even when the service is DOWN

1 participant