From 790d77756e9aafd9d15bd3640f6b4952fcec764d Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Tue, 1 Mar 2022 06:19:34 -0500 Subject: [PATCH] app: fix blocking I/O Open and close static files to be read using the default thread pool executor. Signed-off-by: Eric Callahan --- moonraker/app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/moonraker/app.py b/moonraker/app.py index bad50e6..94200e0 100644 --- a/moonraker/app.py +++ b/moonraker/app.py @@ -46,6 +46,7 @@ if TYPE_CHECKING: from confighelper import ConfigHelper from klippy_connection import KlippyConnection as Klippy from components.file_manager.file_manager import FileManager + from io import BufferedReader import components.authorization MessageDelgate = Optional[tornado.httputil.HTTPMessageDelegate] AuthComp = Optional[components.authorization.Authorization] @@ -778,7 +779,8 @@ class FileRequestHandler(AuthorizedFileHandler): start: Optional[int] = None, end: Optional[int] = None ) -> AsyncGenerator[bytes, None]: - with open(abspath, "rb") as file: + file: BufferedReader = await evt_loop.run_in_thread(open, abspath, "rb") + try: if start is not None: file.seek(start) if end is not None: @@ -798,6 +800,8 @@ class FileRequestHandler(AuthorizedFileHandler): if remaining is not None: assert remaining == 0 return + finally: + await evt_loop.run_in_thread(file.close) @classmethod def _get_cached_version(cls, abs_path: str) -> Optional[str]: