fix(rate-limit): fail open on non-RedisError connection failures - #60
Merged
Merged
Conversation
The 'Docker build and integration tests' job failed because
test_complete_batch_lifecycle and test_organization_isolation both
POST to /api/batches (no trailing slash), which FastAPI 307-redirects
to /api/batches/. Starlette's TestClient follows that redirect as a
second internal ASGI call that runs on a fresh event loop, while the
rate-limit middleware's Redis client is a long-lived object created
once at import time. redis-py's asyncio transport detects the pooled
connection is now attached to a different event loop and raises a bare
RuntimeError ('Event loop is closed') instead of a redis.RedisError
subclass.
create_rate_limit_middleware only caught redis.RedisError, so this
RuntimeError escaped as an unhandled 500, defeating the module's
documented 'fail open when Redis is unavailable' contract and failing
both integration tests.
Broaden the fail-open handling to also catch OSError/RuntimeError from
the Redis client, and add a regression test covering all three failure
shapes (ConnectionError, RuntimeError, OSError) against the middleware
directly.
Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Problem
The
docker-integrationCI job ("Docker build and integration tests") failed with:Root cause
Both failing tests
POST /api/batches(no trailing slash). FastAPI 307-redirects that to/api/batches/, and Starlette'sTestClientfollows the redirect as a second internal ASGI call that runs on a fresh event loop.The rate-limit middleware (
backend/app/core/rate_limit.py) holds a single long-lived async Redis client created once at import time. When the second call's Redis operation reuses a pooled connection from the first event loop, redis-py's asyncio transport detects the mismatch and raises a bareRuntimeError("Event loop is closed")instead of aredis.RedisErrorsubclass.The middleware only caught
redis.RedisErrorto implement its documented "fail open if Redis is unreachable" behavior, so thisRuntimeErrorescaped as an unhandled exception, turning into a 500 and failing both integration tests.Fix
create_rate_limit_middlewareto also catchOSError/RuntimeError(transport-level failures and event-loop mismatches), matching the module's stated contract that Redis problems must never break a request.test_middleware_fails_open_when_redis_is_unreachable) covering all three failure shapes:redis.ConnectionError,RuntimeError, andOSError.Verification
requirements.lockversions and a real Redis server (byte-for-byte match of the reported error).test_complete_batch_lifecycleandtest_organization_isolationnow pass.flake8,mypy app, and the broader pytest suite show no regressions from this change.