[bug] Test project RedisChannelLayer times out after 5s with redis-py 8
Describe the bug
In tests/openwisp2/settings.py (lines 106-112), when not TESTING, CHANNEL_LAYERS configures channels_redis.core.RedisChannelLayer with a plain Redis URL string:
if not TESTING:
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {"hosts": [f"{REDIS_URL}/7"]},
}
}
Starting with redis-py 8.0.0, the default socket timeout is 5 seconds. Because channels_redis blocks on BZPOPMIN for 5 seconds while waiting for messages on empty channels (see django/channels_redis#422), RedisChannelLayer.receive() raises redis.exceptions.TimeoutError: Timeout reading from ... after 5 seconds and Daphne closes active WebSocket connections with close code 1011.
This is the same redis-py 8 channel layer timeout issue already fixed in openwisp-notifications (bb156cf71) and reported in openwisp/openwisp-firmware-upgrader#517.
Steps To Reproduce
- Install test dependencies (
pip install -r requirements-test.txt) withredis>=8.0.0andchannels_redisand start a local Redis server. - Run the test project (
python tests/manage.py runserver). - Open a page using a WebSocket consumer backed by
RedisChannelLayer(or callawait RedisChannelLayer(hosts=["redis://localhost:6379/7"]).receive(channel)on an empty channel). - Observe
redis.exceptions.TimeoutError/ WebSocket close code1011after ~5 seconds.
Expected behavior
WebSocket connections remain open while waiting for channel messages. Updating the host configuration in tests/openwisp2/settings.py to:
"CONFIG": {
"hosts": [{"address": f"{REDIS_URL}/7", "socket_timeout": None}],
}
prevents the 5-second BZPOPMIN socket timeout while preserving Redis channel layer behavior.
System Information:
- Branch:
master - File:
tests/openwisp2/settings.py - Affected dependency:
redis-py >= 8.0.0withchannels_redis
I have the fix ready for tests/openwisp2/settings.py (setting "socket_timeout": None in CHANNEL_LAYERS matching openwisp-notifications) and will open the PR as soon as a maintainer validates this issue. Thanks!