Refs #33173, Refs #30451 -- Fixed ResourceWarning from unclosed body files in ASGI handler on Python 3.11+.

This commit is contained in:
Mariusz Felisiak 2022-05-10 09:57:28 +02:00 committed by GitHub
parent 439cd73c16
commit 441103a04d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -164,12 +164,15 @@ class ASGIHandler(base.BaseHandler):
except RequestAborted: except RequestAborted:
return return
# Request is complete and can be served. # Request is complete and can be served.
set_script_prefix(self.get_script_prefix(scope)) try:
await sync_to_async(signals.request_started.send, thread_sensitive=True)( set_script_prefix(self.get_script_prefix(scope))
sender=self.__class__, scope=scope await sync_to_async(signals.request_started.send, thread_sensitive=True)(
) sender=self.__class__, scope=scope
# Get the request and check for basic issues. )
request, error_response = self.create_request(scope, body_file) # Get the request and check for basic issues.
request, error_response = self.create_request(scope, body_file)
finally:
body_file.close()
if request is None: if request is None:
await self.send_response(error_response, send) await self.send_response(error_response, send)
return return
@ -192,6 +195,7 @@ class ASGIHandler(base.BaseHandler):
while True: while True:
message = await receive() message = await receive()
if message["type"] == "http.disconnect": if message["type"] == "http.disconnect":
body_file.close()
# Early client disconnect. # Early client disconnect.
raise RequestAborted() raise RequestAborted()
# Add a body chunk from the message, if provided. # Add a body chunk from the message, if provided.