From 1b5338d03ecc962af8ab4678426bc60b0672b8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Va=C5=A1ek=20Dohnal?= Date: Thu, 8 Feb 2024 09:21:03 +0100 Subject: [PATCH] Fixed #35174 -- Fixed Signal.asend()/asend_robust() crash when all receivers are asynchronous. Regression in e83a88566a71a2353cebc35992c110be0f8628af. --- django/dispatch/dispatcher.py | 8 ++++++-- docs/releases/5.0.3.txt | 4 ++++ tests/signals/tests.py | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/django/dispatch/dispatcher.py b/django/dispatch/dispatcher.py index 26ef09ce49ae..fe0e1fa59933 100644 --- a/django/dispatch/dispatcher.py +++ b/django/dispatch/dispatcher.py @@ -244,7 +244,9 @@ def sync_send(): return responses else: - sync_send = list + + async def sync_send(): + return [] responses, async_responses = await asyncio.gather( sync_send(), @@ -380,7 +382,9 @@ def sync_send(): return responses else: - sync_send = list + + async def sync_send(): + return [] async def asend_and_wrap_exception(receiver): try: diff --git a/docs/releases/5.0.3.txt b/docs/releases/5.0.3.txt index 384ce27fb737..30e87127b0b7 100644 --- a/docs/releases/5.0.3.txt +++ b/docs/releases/5.0.3.txt @@ -11,3 +11,7 @@ Bugfixes * Fixed a regression in Django 5.0.2 where ``intcomma`` template filter could return a leading comma for string representation of floats (:ticket:`35172`). + +* Fixed a bug in Django 5.0 that caused a crash of ``Signal.asend()`` and + ``asend_robust()`` when all receivers were asynchronous functions + (:ticket:`35174`). diff --git a/tests/signals/tests.py b/tests/signals/tests.py index 5558778bbe90..6c90c6aa5216 100644 --- a/tests/signals/tests.py +++ b/tests/signals/tests.py @@ -626,3 +626,19 @@ async def failing_async_handler(**kwargs): (async_handler, 1), ], ) + + async def test_asend_only_async_receivers(self): + async_handler = AsyncHandler() + signal = dispatch.Signal() + signal.connect(async_handler) + + result = await signal.asend(self.__class__) + self.assertEqual(result, [(async_handler, 1)]) + + async def test_asend_robust_only_async_receivers(self): + async_handler = AsyncHandler() + signal = dispatch.Signal() + signal.connect(async_handler) + + result = await signal.asend_robust(self.__class__) + self.assertEqual(result, [(async_handler, 1)])