Skip to content

Django signal handling hard coded for ASGI but breaks for WSGI #88

@joshpwrk

Description

@joshpwrk

Problem:
When using WSGI servers with django_valkey you get the following error:
/usr/local/lib/python3.10/site-packages/django/http/response.py:335: RuntimeWarning: coroutine 'close_async_caches' was never awaited

Reason:
The current implementation connects an async connection closer to request_finished, which creates un-awaited coroutines in WSGI environments where no event loop exists. Although the implementation checks whether the Valkey object is_async, the function itself is defined as await def and thus throws an error.

Furthermore, even if users set CLOSE_CONNECTION = False, this does not prevent django from running the buggy close_async_caches function.

Solution:
Create separate sync and async handlers, and only connect the async version when async backends are detected. I created a PR (#87) for this and would appreciate the merge as we've had to manually override this in our codebase (see below). This won't work for users that set CLOSE_CONNECTION=True

Temp override that users would need to do:

from django.core import signals as django_signals

try:
    from django_valkey.base import close_async_caches

    django_signals.request_finished.disconnect(close_async_caches)
    logger.info("Disconnected django-valkey async cache closing signal for WSGI mode")
except (ImportError, AttributeError) as e:
    logger.warning(f"Could not disconnect django-valkey signal: {e}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions