[Django] #36081: HttpResponse 204 (No Content) causes client code to hang if data is present.

29 views
Skip to first unread message

Django

unread,
Jan 10, 2025, 11:54:52 AM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Type:
| Uncategorized
Status: new | Component: HTTP
| handling
Version: 4.2 | Severity: Normal
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
**What Happened**
I discovered this when returning "{}" as the body for a 204 status
response.

**Explanation**
When returning an HttpResponse to the client with a status of 204 (no
content), their system will hang if data is actually sent back in the
body.
It does this because it's an invalid response and it breaks the HTTP spec.
Clients will hang until a valid response is returned from the server.

https://developer.mozilla.org/en-
US/docs/Web/HTTP/Status/204#compatibility_notes

"Although this status code is intended for responses with no body, servers
may erroneously include data after the headers. This issue is observable
in persistent connections, where the invalid body may include a distinct
response to a subsequent request. The HTTP protocol allows browsers to
handle such responses differently (there is an ongoing discussion
regarding the specification text in the HTTPWG http-core GitHub
repository).

Apple Safari rejects any such data. Google Chrome and Microsoft Edge
discard up to four invalid bytes preceding a valid response. Firefox
tolerates over a kilobyte of invalid data preceding a valid response."

**Examples**
1. This is observed in Postman and other systems, the response is already
returned but it's still "Receiving Response". In this case, it will
timeout after a minute. Browsers may handle it differently.
2. My webhook made the client code timeout and forced the webhook into a
retry sequence.

**Solution**
I think Django should raise an exception in the HttpResponse class if this
happens. Browsers take their own approach on what to do in this scenario
and an opinionated stance may be warranted. It will also save developers
from bizarre behavior.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 10, 2025, 11:57:56 AM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: Uncategorized | Status: new
Component: HTTP handling | Version: 4.2
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Cleophus Robinson):

* Attachment "ReceivingResponse.png" added.

Django

unread,
Jan 10, 2025, 12:05:06 PM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: Uncategorized | Status: new
Component: HTTP handling | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Cleophus Robinson):

* version: 4.2 =>

--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:1>

Django

unread,
Jan 10, 2025, 8:46:53 PM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: Jacob
| Austin Lincoln
Type: Uncategorized | Status: assigned
Component: HTTP handling | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Austin Lincoln):

* owner: (none) => Jacob Austin Lincoln
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:2>

Django

unread,
Jan 10, 2025, 8:49:02 PM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: Uncategorized | Status: new
Component: HTTP handling | Version:
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Austin Lincoln):

* owner: Jacob Austin Lincoln => (none)
* status: assigned => new

--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:3>

Django

unread,
Jan 10, 2025, 11:23:57 PM1/10/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
--------------------------------------+------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: Uncategorized | Status: new
Component: HTTP handling | Version:
Severity: Normal | Resolution:
Keywords: 204,HTTP,API,Timeout | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Cleophus Robinson):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:4>

Django

unread,
Jan 11, 2025, 4:45:23 AM1/11/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: Bug | Status: new
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Unreviewed
* type: Uncategorized => Bug
* version: => 5.1

Comment:

Hi Cleophus — please don't "Accept" your own tickets. It needs to be
triaged by someone other than the reporter. Thanks.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:5>

Django

unread,
Jan 11, 2025, 4:48:23 AM1/11/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: New feature | Status: new
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* type: Bug => New feature

Comment:

Marking this as a New Feature — Reading the report, it's the client that
needs to handle this.

Best approach is for users to not send data when using 204 status codes. A
project-level response class would be my suggestion.

I'm sceptical about adding something for this in Django itself. I'd argue
for wontfix probably.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:6>

Django

unread,
Jan 11, 2025, 9:49:35 AM1/11/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: (none)
Type: New feature | Status: new
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Aditya Upadhye):

Hi Cleophus Robinson, this looks like additional handling for HTTP 204. I
would like to work on this.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:7>

Django

unread,
Jan 11, 2025, 1:44:11 PM1/11/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: Aditya
| Upadhye
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Aditya Upadhye):

* owner: (none) => Aditya Upadhye
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:8>

Django

unread,
Jan 12, 2025, 3:56:41 AM1/12/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: Aditya
| Upadhye
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Cleophus Robinson):

Whoops, I was confused by the "next steps" message above - I'll know for
next time.

If I'm understanding correctly the original suggestion was for us to
handle it at the project level? Or by project level do you mean another,
more strict class within the django.http module? My team and I won't make
this mistake again so the fix isn't so much for us. I'm more worried about
the bizarreness of it as it will certainly trip up other developers. For a
little more context, we were using it in our JSON API and returning "{}"
for a 200 response. We changed this to a 204 later on and thought nothing
of it - to our surprise.

I do think this looks like additional handling / validation for 204. The
lowest hanging fruit would be a warning in the documentation but it
wouldn't be as effective. The issue is too innocuous for someone to
suspect a 2xx status code as the root cause.

In any case, thank you both for taking a look! Let me know if I can assist
in any way.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:9>

Django

unread,
Jan 13, 2025, 9:52:23 AM1/13/25
to django-...@googlegroups.com
#36081: HttpResponse 204 (No Content) causes client code to hang if data is
present.
-------------------------------------+-------------------------------------
Reporter: Cleophus Robinson | Owner: Aditya
| Upadhye
Type: New feature | Status: closed
Component: HTTP handling | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
204,HTTP,API,Timeout | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* resolution: => wontfix
* status: assigned => closed
* version: 5.1 => dev

Comment:

Thank you all for the discussion and the context surrounding the report.
I've reviewed the relevant sections of the RFC and done some additional
research. Following that, I think that ensuring a proper 204 response is
the responsibility of the project itself. For reference, Flask doesn't
implement any special handling for 204 responses, and Django doesn't have
a custom HttpResponse class for 20x status codes. Additionally, I believe
clients should ideally be more resilient and not hang on 204 responses
that include content.
--
Ticket URL: <https://code.djangoproject.com/ticket/36081#comment:10>
Reply all
Reply to author
Forward
0 new messages