Skip to content

Commit

Permalink
Bugfix ensure request files are closed
Browse files Browse the repository at this point in the history
By adding a close method to requests and call on cleanup. This mirrors
the method in Flask and ensures that any files opened for the request
e.g. when parsing body data files are closed.
  • Loading branch information
pgjones committed Feb 11, 2024
1 parent 32698f3 commit 1fb23b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/quart/ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ async def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ign
if exc is _sentinel:
exc = sys.exc_info()[1]
await self.app.do_teardown_request(exc, self)

request_close = getattr(self.request_websocket, "close", None)
if request_close is not None:
await request_close()
finally:
ctx = _cv_request.get()
token, app_ctx = self._cv_tokens.pop()
Expand Down
6 changes: 5 additions & 1 deletion src/quart/wrappers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, AnyStr, Awaitable, Callable, Generator, NoReturn, overload

from hypercorn.typing import HTTPScope
from werkzeug.datastructures import CombinedMultiDict, Headers, MultiDict
from werkzeug.datastructures import CombinedMultiDict, Headers, iter_multi_items, MultiDict
from werkzeug.exceptions import BadRequest, RequestEntityTooLarge, RequestTimeout

from .base import BaseRequestWebsocket
Expand Down Expand Up @@ -346,3 +346,7 @@ async def send_push_promise(self, path: str) -> None:
for value in self.headers.getlist(name):
headers.add(name, value)
await self._send_push_promise(path, headers)

async def close(self) -> None:
for _key, value in iter_multi_items(self._files or ()):
value.close()

0 comments on commit 1fb23b7

Please sign in to comment.