Opened 5 hours ago

Last modified 5 hours ago

#36081 new Uncategorized

HttpResponse 204 (No Content) causes client code to hang if data is present.

Reported by: Cleophus Robinson Owned by:
Component: HTTP handling Version:
Severity: Normal Keywords: 204, HTTP, API, Timeout
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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.

Attachments (1)

ReceivingResponse.png (89.7 KB ) - added by Cleophus Robinson 5 hours ago.

Download all attachments as: .zip

Change History (2)

by Cleophus Robinson, 5 hours ago

Attachment: ReceivingResponse.png added

comment:1 by Cleophus Robinson, 5 hours ago

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