Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#34174 closed Cleanup/optimization (invalid)

async process_exception being called as sync from async view/middleware

Reported by: Gabriel Rado Owned by: Gabriel Rado
Component: Documentation Version: 4.1
Severity: Normal Keywords: middleware async process_exception
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gabriel Rado)

following this bold part from https://docs.djangoproject.com/en/4.1/topics/http/middleware/#asynchronous-support:

process_view, process_template_response and process_exception methods, if they are provided, should also be adapted to match the sync/async mode. However, Django will individually adapt them as required if you do not, at an additional performance penalty.

I'm using this middleware to parse errors from sync and async views:

@sync_and_async_middleware
def custom_error(get_response):
    if asyncio.iscoroutinefunction(get_response):

        async def middleware(request):
            response = await get_response(request)
            return response

        async def _process_exception(request, exception):
            return my_custom_process_exception(exception)

    else:

        def middleware(request):
            response = get_response(request)
            return response

        def _process_exception(request, exception):
            return my_custom_process_exception(exception)

    middleware.process_exception = _process_exception
    return middleware

and I'm getting "ValueError: The view my_view.view_func didn't return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an 'await' into your view." from https://github.com/django/django/blob/main/django/core/handlers/base.py#L257

i thought that with an async middleware + async view, all my hooks (process_view, process_template_response and process_exception) should be also async, but it appears that is mandatory to use sync process_exception with async capable middlewares. if this is intended, could the official docs be changed to expose this mandatory behavior?

Change History (5)

comment:1 by Gabriel Rado, 18 months ago

Description: modified (diff)

comment:2 by Carlton Gibson, 18 months ago

Triage Stage: UnreviewedAccepted

Hi Gabriel. OK, seems right, are you happy to suggest a docs adjustment? (… but if you could add a test to demonstrate what you're seeing explicitly, that would be good too.) Thanks.

comment:3 by Gabriel Rado, 18 months ago

Owner: changed from nobody to Gabriel Rado
Status: newassigned

comment:4 by Gabriel Rado, 18 months ago

Resolution: invalid
Status: assignedclosed

i tried to create a test to demonstrate this behavior and every try was successfull. thinking the problem could be another point of my project and not django itself, i'll close this ticket

comment:5 by Carlton Gibson, 18 months ago

OK, thanks for the follow-up. Do reopen if you're able to reduce it to an issue in Django.

Note: See TracTickets for help on using tickets.
Back to Top