Skip to content

Memory leak with redis backend when hiredis is installed #153

Description

@jamestrousdale

The problem

Not so much a bug with aiohttp-client-cache per se, but it can be impacted by upstream bugs. There may also be a chance for optimization.

I filed redis/redis-py#2741 against apparent memory leaks from redis-py when hiredis is installed.

As part of the investigation, I realized that even if we provide our own Redis connection instance to aiohttp-client-cache, it still takes the liberty to close the connection on our behalf after the HTTP request session ends.

I think that if the connection object is supplied (as opposed to the redis URL), then aiohttp-client-cache should not close the Redis connection, as the client code may want to continue to use that connection object/associated connection pool.

The problem is not big in one-off scripts, but in a long-running microservice (where we discovered the issue originally), it will definitely lead to OOMs.

Expected behavior

If the user supplies a Redis object in lieu of the URL, don't close connections.

Steps to reproduce the behavior

See redis/redis-py#2741 - the connections created as part of the RedisBackend objects needed for Redis-backed response caching should make it obvious how to reproduce in aiohttp-client-cache

Workarounds

Don't use hiredis when using redis backend

Environment

  • aiohttp-client-cache version: 0.8.1
  • Python version: 3.11.3
  • Platform: Reproduced on both MacOS (arm64) and Linux (amd64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions