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

 Hi Jonas.

 I was digging a bit more here:

 > ...unfortunately the HttpRequest.body property drops the reference to
 the body_file by overwriting it with a BytesIO.

 Not closing `self._stream` looks like we should be able to fix in general
 (i.e. it's not ASGI-specific 🤔)

 This passes:

 {{{
 diff --git a/django/http/request.py b/django/http/request.py
 index 4b160bc5f4..8354dfe57b 100644
 --- a/django/http/request.py
 +++ b/django/http/request.py
 @@ -340,6 +340,11 @@ class HttpRequest:
                  self._body = self.read()
              except OSError as e:
                  raise UnreadablePostError(*e.args) from e
 +            finally:
 +                try:
 +                    self._stream.close()
 +                except:
 +                    pass
              self._stream = BytesIO(self._body)
          return self._body
 }}}

 This in `body`, since `read` can be used passing a chunk size, and in the
 `try...except` because WSGIHandler uses a `LimitedStream` wrapper that
 doesn't (currently) have a  `close`.

 Then, adding a simple `close()` to `ASGIRequest`...

 {{{
    def close(self):
         super().close()
         self._stream.close()
 }}}

 ... and blocking out the `body_file.close()` in `handle()` clears up all
 the ResourceWarnings except two:

 * `test_static_file_response` — This because the `ASGIStaticFilesHandler`
 wraps `ASGIHandler` so (likely) isn't calling `response.close` correctly.
 (See also #33048, #27325)
 * `test_non_unicode_query_string` — We're not then handling the `close()`
 when there's a Unicode error in `create_request()`.

 Both of those should be addressable though.

 What do you think?

 Do you fancy continuing on this?

 Thanks!

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33755#comment:8>
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/0107018123db3924-446e17f7-96fc-4db9-a3d5-8f3b8b27f9be-000000%40eu-central-1.amazonses.com.

Reply via email to