openwisp-controller icon indicating copy to clipboard operation
openwisp-controller copied to clipboard

[bug] Test project RedisChannelLayer times out after 5s with redis-py 8

Open Sauvi72 opened this issue 1 week ago • 1 comments

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

  1. Install test dependencies (pip install -r requirements-test.txt) with redis>=8.0.0 and channels_redis and start a local Redis server.
  2. Run the test project (python tests/manage.py runserver).
  3. Open a page using a WebSocket consumer backed by RedisChannelLayer (or call await RedisChannelLayer(hosts=["redis://localhost:6379/7"]).receive(channel) on an empty channel).
  4. Observe redis.exceptions.TimeoutError / WebSocket close code 1011 after ~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.0 with channels_redis

Sauvi72 avatar Nov 24 '26 20:11 Sauvi72

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!

Sauvi72 avatar Nov 24 '26 20:11 Sauvi72