fix(django): Avoid ValueError in async middleware process_* hooks#6698
Merged
alexander-alderman-webb merged 1 commit intoJun 30, 2026
Merged
Conversation
process_* hooksValueError in async middleware process_* hooks
auto-merge was automatically disabled
June 30, 2026 14:14
Head branch was pushed to by a user without write access
16903d3 to
42bb137
Compare
When an async Django middleware defines an `async def` hook such as
`process_view` or `process_exception`, the SDK wrapped it in a synchronous
function. That hid the coroutine from Django's `iscoroutinefunction` check in
`BaseHandler.load_middleware`, so Django wrapped the hook in `SyncToAsync` and
called it synchronously. The underlying coroutine was then never awaited, and
the request failed with:
```
ValueError: The view ... didn't return an HttpResponse object.
It returned an unawaited coroutine instead.
```
`_get_wrapped_method` in the Django middleware integration now produces an
`async def` wrapper when the wrapped middleware hook is a coroutine function,
instead of always producing a synchronous wrapper.
auto-merge was automatically disabled
June 30, 2026 14:28
Head branch was pushed to by a user without write access
42bb137 to
8e9fb08
Compare
alexander-alderman-webb
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When an async Django middleware defines an
async defhook such asprocess_vieworprocess_exception, the SDK wrapped it in a synchronous function. That hid the coroutine from Django'siscoroutinefunctioncheck inBaseHandler.load_middleware, so Django wrapped the hook inSyncToAsyncand called it synchronously. The underlying coroutine was then never awaited, and the request failed with:_get_wrapped_methodin the Django middleware integration now produces anasync defwrapper when the wrapped middleware hook is a coroutine function, instead of always producing a synchronous wrapper.Issues
Discussed on Discord https://discord.com/channels/621778831602221064/1521260879218348103