#33755: Move ASGI body-file cleanup into ASGIRequest
-------------------------------------+-------------------------------------
     Reporter:  Carlton Gibson       |                    Owner:  Jonas
         Type:                       |  Lundberg
  Cleanup/optimization               |                   Status:  assigned
    Component:  HTTP handling        |                  Version:  dev
     Severity:  Normal               |               Resolution:
     Keywords:  ASGI                 |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Jonas Lundberg):

 Looked at this briefly and unfortunately the {{{HttpRequest.body}}}
 property drops the reference to the {{{body_file}}} by overwriting it with
 a {{{BytesIO}}}.

 {{{
 @property
 def body(self):
     if ...
         self._stream = BytesIO(self._body)
     return self._body
 }}}

 I'm thinking it could be safe to close the file in a finally block once it
 have been read ...

 {{{
 diff --git a/django/http/request.py b/django/http/request.py
 index 4b160bc..5e5f349 100644
 --- a/django/http/request.py
 +++ b/django/http/request.py
 @@ -400,14 +400,16 @@ class HttpRequest:

      def read(self, *args, **kwargs):
          self._read_started = True
          try:
              return self._stream.read(*args, **kwargs)
          except OSError as e:
              raise UnreadablePostError(*e.args) from e
 +        finally:
 +            self._stream.close()
 }}}

 At least the test from #33754 still succeeds ;-)

 Thoughts?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33755#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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070181192701d4-8c0d1279-eed5-400e-90a8-293e17bc1eea-000000%40eu-central-1.amazonses.com.

Reply via email to