From 123db8b5096e6108b524eae365b71958ea560b45 Mon Sep 17 00:00:00 2001 From: philippe Date: Tue, 24 Nov 2026 10:29:49 -0500 Subject: [PATCH 1/2] Wait for a free connection in RedisSharedStorage's async pool instead of failing --- .ai/ARCHITECTURE.md | 5 +++- CHANGELOG.md | 5 ++++ dash/_shared_storage/redis.py | 35 +++++++++++++++------- tests/shared_storage/test_redis_backend.py | 23 ++++++++++++++ 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/.ai/ARCHITECTURE.md b/.ai/ARCHITECTURE.md index 5cae8f69bc..d0c09b651e 100644 --- a/.ai/ARCHITECTURE.md +++ b/.ai/ARCHITECTURE.md @@ -1006,7 +1006,10 @@ multi-stream `XREAD`; a new subscription interrupts it through a private wake stream. Never park a blocking read in the executor per subscription: at a dozen open downlinks that starved every other store call in the worker (25-50% failed streams at 25 browsers on uvicorn `--workers 4`). Diskcache's async -subscriptions likewise poll without blocking and sleep on the loop. +subscriptions likewise poll without blocking and sleep on the loop. Each loop's +client uses a `BlockingConnectionPool` of 100 that waits up to 20s for a free +connection: redis-py 8 caps a plain pool at 100 and raises past it, which +failed 7% of streams at 2000 browsers on 4 uvicorn workers. ### Deployment Topology and Shared Storage diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c33f9cf..5dcdc9e2fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `dash` will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/). +## [UNRELEASED] + +### Fixed +- [#]() Fix `RedisSharedStorage` streaming callbacks failing under load with redis-py 8 (`MaxConnectionsError: Too many connections`): async calls now wait for a free connection. + ## [4.5.1] - 2026-11-19 ### Removed diff --git a/dash/_shared_storage/redis.py b/dash/_shared_storage/redis.py index 6c3743ef47..66b8bd8687 100644 --- a/dash/_shared_storage/redis.py +++ b/dash/_shared_storage/redis.py @@ -45,6 +45,10 @@ _READER_BLOCK_MS = 5000 _WAKE_TTL_MS = 60_000 _DEFAULT_URL = "redis://localhost:6379" +# Connections per event loop's async client, and how long a call waits for a +# free one before it fails. +_ASYNC_POOL_SIZE = 100 +_ASYNC_POOL_WAIT = 20.0 # A topic's keys live this many ttls past its last publish or poll. A poll on a # topic with a ttl blocks for at most a third of that lifetime, so a waiting @@ -431,18 +435,29 @@ def _new_async_client(self) -> Any: _require_redis() from redis import asyncio as aredis # type: ignore[import-not-found,import-untyped] # pylint: disable=import-outside-toplevel,import-error + # redis-py 8 caps a pool at 100 connections and raises past that, so a + # burst of pumps publishing at once failed. A blocking pool waits for a + # free connection instead, and keeps the count Redis sees bounded. + limits = { + "max_connections": _ASYNC_POOL_SIZE, + "timeout": _ASYNC_POOL_WAIT, + } if self._url is not None: - return aredis.Redis.from_url(self._url) - # Same server and options as the client we were handed. - pool = self._redis.connection_pool - connection_class = getattr( - aredis.connection, pool.connection_class.__name__, aredis.Connection - ) - return aredis.Redis( - connection_pool=aredis.ConnectionPool( - connection_class=connection_class, **pool.connection_kwargs + pool = aredis.BlockingConnectionPool.from_url(self._url, **limits) + else: + # Same server and options as the client we were handed. + sync_pool = self._redis.connection_pool + connection_class = getattr( + aredis.connection, + sync_pool.connection_class.__name__, + aredis.Connection, ) - ) + pool = aredis.BlockingConnectionPool( + connection_class=connection_class, + **limits, + **sync_pool.connection_kwargs, + ) + return aredis.Redis(connection_pool=pool) def _loop_state(self) -> "_LoopState": loop = asyncio.get_running_loop() diff --git a/tests/shared_storage/test_redis_backend.py b/tests/shared_storage/test_redis_backend.py index b51c032034..ed61745c71 100644 --- a/tests/shared_storage/test_redis_backend.py +++ b/tests/shared_storage/test_redis_backend.py @@ -293,6 +293,29 @@ async def scenario(): client.close() +@pytest.mark.parametrize("passed_client", [False, True]) +def test_async_burst_larger_than_the_pool_waits_for_connections(passed_client): + """redis-py 8 caps a pool at 100 connections and raises past it. A burst of + pumps publishing at once must wait for a connection, not fail.""" + client = redis.Redis.from_url(REDIS_URL) if passed_client else None + store = RedisSharedStorage( + url=None if passed_client else REDIS_URL, + client=client, + key_prefix=f"dash:sstest:{uuid.uuid4().hex[:12]}", + ) + store.start() + + async def scenario(): + await asyncio.gather(*(store.apublish("burst", i) for i in range(500))) + await asyncio.gather(*(store.aset(f"k{i}", i) for i in range(500))) + + asyncio.run(scenario()) + assert store.get("k499") == 499 + store.close() + if client is not None: + client.close() + + @pytest.mark.parametrize("flavor", ["sync", "async"]) def test_quiet_topic_outlasts_the_socket_timeout(flavor): """redis-py 8 times sockets out after 5 s, as long as an XREAD block: a From 574139b1f386c56884460cbf63097921ea4b04c2 Mon Sep 17 00:00:00 2001 From: philippe Date: Tue, 24 Nov 2026 10:30:02 -0500 Subject: [PATCH 2/2] Add PR number to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dcdc9e2fd..b8e4005a3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [UNRELEASED] ### Fixed -- [#]() Fix `RedisSharedStorage` streaming callbacks failing under load with redis-py 8 (`MaxConnectionsError: Too many connections`): async calls now wait for a free connection. +- [#4185](https://github.com/plotly/dash/pull/4185) Fix `RedisSharedStorage` streaming callbacks failing under load with redis-py 8 (`MaxConnectionsError: Too many connections`): async calls now wait for a free connection. ## [4.5.1] - 2026-11-19