From bd389a832aa1480fc76c3fb2bb6f92ea4209e378 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 28 May 2020 13:05:15 +0200 Subject: [PATCH] [3.1.x] Refs #31040, Refs #31224 -- Prevented cycles in exceptions chain. Async exception handling was raising an exception that was creating a cycle in the exception chain (by re-raising an exception in sync_to_async that was already being handled). Thanks Chris Jerdonek for detailed analysis. Backport of d94a9aa0557a459a5b9b7b82a8c043de14f8b1a0 from master --- django/core/handlers/base.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index e7fbaf594e2..ba7deb601bc 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -179,6 +179,8 @@ class BaseHandler: response = wrapped_callback(request, *callback_args, **callback_kwargs) except Exception as e: response = self.process_exception_by_middleware(e, request) + if response is None: + raise # Complain if the view returned None (a common error). self.check_response(response, callback) @@ -200,6 +202,8 @@ class BaseHandler: response = response.render() except Exception as e: response = self.process_exception_by_middleware(e, request) + if response is None: + raise return response @@ -230,6 +234,8 @@ class BaseHandler: self.process_exception_by_middleware, thread_sensitive=True, )(e, request) + if response is None: + raise # Complain if the view returned None or an uncalled coroutine. self.check_response(response, callback) @@ -258,6 +264,8 @@ class BaseHandler: self.process_exception_by_middleware, thread_sensitive=True, )(e, request) + if response is None: + raise # Make sure the response is not a coroutine if asyncio.iscoroutine(response): @@ -323,13 +331,13 @@ class BaseHandler: def process_exception_by_middleware(self, exception, request): """ Pass the exception to the exception middleware. If no middleware - return a response for this exception, raise it. + return a response for this exception, return None. """ for middleware_method in self._exception_middleware: response = middleware_method(request, exception) if response: return response - raise + return None def reset_urlconf(sender, **kwargs):