Skip to content

[BUG] Redis connection failed: Error 22 connecting to localhost:6379 #89

Closed

Description

@CatamountJack

Bug Description

Upgraded from fastapi-guard 5.0.0 to 7.0.0 and began receiving a Redis connection failed: Error 22 connecting to localhost:6379 error upon the first connection.

Downgraded and found that 5.1.0 works, but the issue appears beginning with 5.1.1. No changes to configuration.

I'm not using Redis directly anywhere in my application, nor do I have it configured for fastapi-guard at all.


Steps To Reproduce

Steps to reproduce the behavior:

  1. Configure FastAPI Guard with (see MRE below)
  2. Make request to endpoint (see MRE below)
  3. See error

Expected Behavior

Normal connection


Actual Behavior

What actually happened, including error messages, stack traces, or logs.

Server returned <Response [500 Internal Server Error]> to client upon connection attempt.

Traceback:

[guard_core.handlers.redis] 2026-05-08 08:01:20,890 - ERROR - Redis connection failed: Error 22 connecting to localhost:6379. The remote computer refused the network connection.
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 371, in connect_check_health
    await self._connect()
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 895, in _connect
    reader, writer = await asyncio.open_connection(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        **self._connection_arguments()
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\streams.py", line 48, in open_connection
    transport, _ = await loop.create_connection(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        lambda: protocol, host, port, **kwds)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\base_events.py", line 1173, in create_connection
    raise exceptions[0]
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\base_events.py", line 1143, in create_connection
    sock = await self._connect_sock(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
        exceptions, addrinfo, laddr_infos)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\base_events.py", line 1042, in _connect_sock
    await self.sock_connect(sock, address)
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\proactor_events.py", line 728, in sock_connect
    return await self._proactor.connect(sock, address)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\windows_events.py", line 804, in _poll
    value = callback(transferred, key, ov)
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\asyncio\windows_events.py", line 600, in finish_connect
    ov.getresult()
    ~~~~~~~~~~~~^^
ConnectionRefusedError: [WinError 1225] The remote computer refused the network connection

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\dev\.venv\Lib\site-packages\guard_core\handlers\redis_handler.py", line 71, in initialize
    await self._redis.ping()
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\client.py", line 764, in execute_command
    conn = self.connection or await pool.get_connection()
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\redis\utils.py", line 227, in async_wrapper
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 1448, in get_connection
    await self.ensure_connection(connection)
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 1489, in ensure_connection
    await connection.connect()
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 340, in connect
    await self.retry.call_with_retry(
    ...<7 lines>...
    )
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\retry.py", line 81, in call_with_retry
    raise error
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\retry.py", line 69, in call_with_retry
    return await do()
           ^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\redis\asyncio\connection.py", line 397, in connect_check_health
    raise e
redis.exceptions.ConnectionError: Error 22 connecting to localhost:6379. The remote computer refused the network connection.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\dev\.venv\Lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 421, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        self.scope, self.receive, self.send
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\dev\.venv\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 56, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\fastapi\applications.py", line 1159, in __call__
    await super().__call__(scope, receive, send)
  File "C:\dev\.venv\Lib\site-packages\starlette\applications.py", line 90, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\errors.py", line 176, in __call__
    response = await self.handler(request, exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\nicegui\nicegui.py", line 191, in _exception_handler_500
    raise exception  # Simply return "Internal Server Error", just like FastAPI would do
    ^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\sessions.py", line 88, in __call__
    await self.app(scope, receive, send_wrapper)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    with recv_stream, send_stream, collapse_excgroups():
                                   ~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
    raise exc
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 193, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\nicegui\storage.py", line 39, in dispatch
    response = await call_next(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 168, in call_next
    raise app_exc from app_exc.__cause__ or app_exc.__context__
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 144, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    with recv_stream, send_stream, collapse_excgroups():
                                   ~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
    raise exc
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 193, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\nicegui\middlewares.py", line 23, in dispatch
    response = await call_next(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 168, in call_next
    raise app_exc from app_exc.__cause__ or app_exc.__context__
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 144, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    with recv_stream, send_stream, collapse_excgroups():
                                   ~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
    raise exc
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 193, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\nicegui\middlewares.py", line 13, in dispatch
    response = await call_next(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 168, in call_next
    raise app_exc from app_exc.__cause__ or app_exc.__context__
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 144, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\gzip.py", line 29, in __call__
    await responder(scope, receive, send)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\gzip.py", line 130, in __call__
    await super().__call__(scope, receive, send)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\gzip.py", line 46, in __call__
    await self.app(scope, receive, self.send_with_compression)
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    with recv_stream, send_stream, collapse_excgroups():
                                   ~~~~~~~~~~~~~~~~~~^^
  File "C:\Users\me\AppData\Roaming\uv\python\cpython-3.14-windows-x86_64-none\Lib\contextlib.py", line 162, in __exit__
    self.gen.throw(value)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\starlette\_utils.py", line 87, in collapse_excgroups
    raise exc
  File "C:\dev\.venv\Lib\site-packages\starlette\middleware\base.py", line 193, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\dev\.venv\Lib\site-packages\guard\middleware.py", line 338, in dispatch
    await self._ensure_initialized()
  File "C:\dev\.venv\Lib\site-packages\guard\middleware.py", line 122, in _ensure_initialized
    await self.initialize()
  File "C:\dev\.venv\Lib\site-packages\guard\middleware.py", line 455, in initialize
    await self.handler_initializer.initialize_redis_handlers()
  File "C:\dev\.venv\Lib\site-packages\guard_core\core\initialization\handler_initializer.py", line 139, in initialize_redis_handlers
    await self.redis_handler.initialize()
  File "C:\dev\.venv\Lib\site-packages\guard_core\handlers\redis_handler.py", line 95, in initialize
    raise GuardRedisError(503, "Redis connection failed") from e
guard_core.exceptions.GuardRedisError: Redis connection failed

Environment

  • FastAPI Guard version: [5.1.1+]
  • Python version: [3.14.4]
  • FastAPI version: [0.136.1]
  • OS: Windows 11

Configuration / MRE

Application (test.py)

# Include your FastAPI Guard configuration here
from fastapi import FastAPI
from guard import SecurityConfig, SecurityMiddleware

security_config = SecurityConfig(
        passive_mode=True,
        enforce_https=False,
        rate_limit=400,
        auto_ban_threshold=5,
        auto_ban_duration=3600,
        enable_penetration_detection=True,
        detection_compiler_timeout=2.0,
        detection_max_content_length=10000,
        detection_preserve_attack_patterns=True,
        detection_semantic_threshold=0.7,
        detection_slow_pattern_threshold=0.1,
        detection_monitor_history_size=1000,
        custom_log_file=r"security_log.txt",
        log_suspicious_level="DEBUG",
        log_request_level="INFO",
        )

app = FastAPI()
app.add_middleware(SecurityMiddleware, config=security_config )

@app.get("/")
def read_root():
    return {"Hello": "World"}

Run the application:

uv run fastapi run --host 127.0.0.1 --port 8080 ./test.py

Connect as client:

import httpx
async with (httpx.AsyncClient() as client):
    response = await client.get("http://127.0.0.1:8080/")

Response:
<Response [500 Internal Server Error]>


Additional Context

Add any other context about the problem here. For example:

  • Is this happening in production or development? development; localhost
  • Does it happen consistently or intermittently? consistently
  • Have you tried any workarounds? no

Activity

  1. CatamountJack commented on May 8, 2026

    @CatamountJack
    Author

    Well, after all that... it appears I simply needed to be explicit with enable_redis=False as part of the SecurityConfig(). Disabling it fixed the issue; apparently it was either ignored or defaulted differently in version 5.1.0 and lower.

  2. rennf93 commented on May 9, 2026

    @rennf93
    Member

    No worries, @CatamountJack ! In any case, I'm always here if you need me :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed defect

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions