#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):

 Okay, I think I understand what's happening now.

 As was remarked above, Django's code has this code comment above
 `HttpResponseBase.close()`:

 {{{
 #!python
 # The WSGI server must call this method upon completion of the request.
 # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-
 on.html
 def close(self):
 }}}

 However, this comment doesn't seem to be true currently in the case that
 `FileResponse` is used with a content stream with a `read()` method. This
 is because in this case, `self.file_to_stream` is set to the content
 stream, and then Django constructs a "file wrapper" from the file (per the
 WSGI spec) instead of using the `HttpResponse` object:

 {{{
 #!python
 if getattr(response, 'file_to_stream', None) is not None and
 environ.get('wsgi.file_wrapper'):
     response = environ['wsgi.file_wrapper'](response.file_to_stream)
 return response
 }}}

 (from:
 
https://github.com/django/django/blob/87f5d07eededc86f8ce1797fdfca7d4903ee0edc/django/core/handlers/wsgi.py#L151-L153
 )

 The implication of this is that when the WSGI server calls `close()` on
 the "file wrapper" object, it only causes `close()` to be called on the
 underlying file-like object and not on the `HttpResponseBase` object.

 So when `FileResponse` is used as I've described above, it doesn't seem
 like `HttpResponseBase.close()` can be used as a hook for the end of the
 request. For this to happen, before Django constructs the "file wrapper"
 object, I think Django would need to modify the file-like object's
 `close()` method  so that it calls `FileResponse.close()`.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30565#comment:6>
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.a37edc47f4386caaf04a81c0476697af%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to