Skip to content

Commit 1fb23b7

Browse files
committed
Bugfix ensure request files are closed
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.
1 parent 32698f3 commit 1fb23b7

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/quart/ctx.py

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ async def pop(self, exc: BaseException | None = _sentinel) -> None: # type: ign
147147
if exc is _sentinel:
148148
exc = sys.exc_info()[1]
149149
await self.app.do_teardown_request(exc, self)
150+
151+
request_close = getattr(self.request_websocket, "close", None)
152+
if request_close is not None:
153+
await request_close()
150154
finally:
151155
ctx = _cv_request.get()
152156
token, app_ctx = self._cv_tokens.pop()

src/quart/wrappers/request.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, AnyStr, Awaitable, Callable, Generator, NoReturn, overload
55

66
from hypercorn.typing import HTTPScope
7-
from werkzeug.datastructures import CombinedMultiDict, Headers, MultiDict
7+
from werkzeug.datastructures import CombinedMultiDict, Headers, iter_multi_items, MultiDict
88
from werkzeug.exceptions import BadRequest, RequestEntityTooLarge, RequestTimeout
99

1010
from .base import BaseRequestWebsocket
@@ -346,3 +346,7 @@ async def send_push_promise(self, path: str) -> None:
346346
for value in self.headers.getlist(name):
347347
headers.add(name, value)
348348
await self._send_push_promise(path, headers)
349+
350+
async def close(self) -> None:
351+
for _key, value in iter_multi_items(self._files or ()):
352+
value.close()

0 commit comments

Comments
 (0)