[Django] #36281: Sync write in async code path

9 views
Skip to first unread message

Django

unread,
Mar 29, 2025, 6:00:41 PM3/29/25
to django-...@googlegroups.com
#36281: Sync write in async code path
--------------------------------+-----------------------------------------
Reporter: Brian Atkinson | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
See
https://github.com/django/django/blob/c1a4fccf53437e480c42648f67277fb7e63ed4ab/django/core/handlers/asgi.py#L266

That write may end up resulting in IO actually hitting disk. This could
result in the async loop stalling if the underlying write call is slow.
--
Ticket URL: <https://code.djangoproject.com/ticket/36281>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 31, 2025, 2:03:34 AM3/31/25
to django-...@googlegroups.com
#36281: Sync write in async code path
--------------------------------+--------------------------------------
Reporter: Brian Atkinson | Owner: (none)
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Comment (by Rayyan Khan):

I think you're right, the sync write in this async code path might cause
problems if the disk I/O is slow. Maybe we could wrap the body_file.write
call with sync_to_async to prevent blocking the event loop?
--
Ticket URL: <https://code.djangoproject.com/ticket/36281#comment:1>

Django

unread,
Mar 31, 2025, 2:25:05 PM3/31/25
to django-...@googlegroups.com
#36281: Sync write in async code path
--------------------------------------+------------------------------------
Reporter: Brian Atkinson | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: HTTP handling | Version: dev
Severity: Normal | Resolution:
Keywords: asgi async | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Natalia Bidart):

* cc: Carlton Gibson (added)
* component: Uncategorized => HTTP handling
* keywords: => asgi async
* stage: Unreviewed => Accepted
* type: Uncategorized => Cleanup/optimization
* version: 5.1 => dev

Comment:

Thank you Brian for the report! I agree that this is worth addressing, as
disk IO can indeed lead to stalls.

Careful consideration will be needed for how to handle the chunked file
writing without blocking the event loop. I'm wondering if we should have a
sort of abstraction for the chunked file writer and allow people to
override. Depending on the project's constraints or current dependencies,
some projects may choose to use a 3rd party library like
[https://github.com/alemigo/asynctempfile asynctempfile] (unclear whether
this is currently maintained), or Django channels, or something else?

We're open to a patch that addresses this while ensuring compatibility and
avoiding unintended regressions. Would you like to work on this?

(I'm torn whether to categorize this as Cleanup or New Feature, for now
I'm going with the former but we can adjust later on.)
--
Ticket URL: <https://code.djangoproject.com/ticket/36281#comment:2>
Reply all
Reply to author
Forward
0 new messages