Skip to content

Commit d94a9aa

Browse files
authored
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.
1 parent fa58450 commit d94a9aa

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

django/core/handlers/base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ def _get_response(self, request):
179179
response = wrapped_callback(request, *callback_args, **callback_kwargs)
180180
except Exception as e:
181181
response = self.process_exception_by_middleware(e, request)
182+
if response is None:
183+
raise
182184

183185
# Complain if the view returned None (a common error).
184186
self.check_response(response, callback)
@@ -200,6 +202,8 @@ def _get_response(self, request):
200202
response = response.render()
201203
except Exception as e:
202204
response = self.process_exception_by_middleware(e, request)
205+
if response is None:
206+
raise
203207

204208
return response
205209

@@ -230,6 +234,8 @@ async def _get_response_async(self, request):
230234
self.process_exception_by_middleware,
231235
thread_sensitive=True,
232236
)(e, request)
237+
if response is None:
238+
raise
233239

234240
# Complain if the view returned None or an uncalled coroutine.
235241
self.check_response(response, callback)
@@ -258,6 +264,8 @@ async def _get_response_async(self, request):
258264
self.process_exception_by_middleware,
259265
thread_sensitive=True,
260266
)(e, request)
267+
if response is None:
268+
raise
261269

262270
# Make sure the response is not a coroutine
263271
if asyncio.iscoroutine(response):
@@ -323,13 +331,13 @@ def make_view_atomic(self, view):
323331
def process_exception_by_middleware(self, exception, request):
324332
"""
325333
Pass the exception to the exception middleware. If no middleware
326-
return a response for this exception, raise it.
334+
return a response for this exception, return None.
327335
"""
328336
for middleware_method in self._exception_middleware:
329337
response = middleware_method(request, exception)
330338
if response:
331339
return response
332-
raise
340+
return None
333341

334342

335343
def reset_urlconf(sender, **kwargs):

0 commit comments

Comments
 (0)