#30565: Close StreamingHttpResponse content immediately after iterating it
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: master
Severity: Normal | Resolution:
Keywords: HttpResponse, | Triage Stage:
streaming, StreamingHttpResponse | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Chris Jerdonek):
Some updates / more info: I'm now focusing more on the question of why
`HttpReponse.close()` isn't being called in my case, since that could
signal an underlying bug.
I believe I'm seeing this behavior only when I use / subclass from
`FileResponse` and not from `StreamingHttpResponse`. Also, I'm using
1.11.20 so haven't confirmed that the issue occurs in master.
The `FileResponse` class has special logic when the `value` argument has a
`read()` method:
{{{
#!python
def _set_streaming_content(self, value):
if hasattr(value, 'read'):
self.file_to_stream = value
filelike = value
if hasattr(filelike, 'close'):
self._closable_objects.append(filelike)
value = iter(lambda: filelike.read(self.block_size), b'')
else:
self.file_to_stream = None
super(FileResponse, self)._set_streaming_content(value)
}}}
(from:
https://github.com/django/django/blob/1c9cb948d7b0c264d244763b6682ab790a6b90a0/django/http/response.py#L411-L420
)
So maybe the reason things start working when I wrap the file object in an
iterator is that it causes `value` not to have a `read()` method anymore,
and so the special logic isn't get executed. So maybe that logic is the
problem.
By the way, is it okay that the value passed to the parent class's
(`StreamingHttpResponse`'s) `_set_streaming_content()` method is different
from the value set to `self.file_to_stream`?
--
Ticket URL: <https://code.djangoproject.com/ticket/30565#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.e77047e89fc7bd0231a9b63e8fcaa00e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.