From 703ec2da4cc2f34dcdaf35a36805ff6bedd1230c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 23 Nov 2023 17:11:46 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Make=20WSGIContainer=20use?= =?UTF-8?q?=20threads=20for=20async=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copied from current tornado.wsgi.WSGIContainer. Seems to solve to not delete the tmp files too early. Also turned UploadStorageFallbackHandler async, otherwise connections would be closed too early, causing issues in E2E and probably also regular use. --- src/octoprint/server/__init__.py | 14 ++- src/octoprint/server/util/tornado.py | 136 ++++++++++++++++++--------- 2 files changed, 102 insertions(+), 48 deletions(-) diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 2fcca6f1b..0f8928f5c 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -677,10 +677,7 @@ class Server: ## Tornado initialization starts here - ioloop = ( - IOLoop() - ) # TODO: This way to create the ioloop is deprecated and logs a warning - ioloop.install() + ioloop = IOLoop.current() enable_cors = settings().getBoolean(["api", "allowCrossOrigin"]) @@ -1020,13 +1017,20 @@ class Server: removed_headers = ["Server"] + from concurrent.futures import ThreadPoolExecutor + server_routes.append( ( r".*", util.tornado.UploadStorageFallbackHandler, { "fallback": util.tornado.WsgiInputContainer( - app.wsgi_app, headers=headers, removed_headers=removed_headers + app.wsgi_app, + executor=ThreadPoolExecutor( + thread_name_prefix="WsgiRequestHandler" + ), + headers=headers, + removed_headers=removed_headers, ), "file_prefix": "octoprint-file-upload-", "file_suffix": ".tmp", diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index a5bf04adb..3ab3bf621 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -20,6 +20,8 @@ import tornado.iostream import tornado.tcpserver import tornado.util import tornado.web +from tornado.concurrent import dummy_executor +from tornado.ioloop import IOLoop from zipstream.ng import ZIP_DEFLATED, ZipStream import octoprint.util @@ -562,7 +564,7 @@ class UploadStorageFallbackHandler(RequestlessExceptionLoggingMixin, CorsSupport self._new_body += value + b"\r\n" self._new_body += b"--%s--\r\n" % self._multipart_boundary - def _handle_method(self, *args, **kwargs): + async def _handle_method(self, *args, **kwargs): """ Takes care of defining the new request body if necessary and forwarding the current request and changed body to the ``fallback``. @@ -582,17 +584,24 @@ class UploadStorageFallbackHandler(RequestlessExceptionLoggingMixin, CorsSupport # directly use data from buffer body = self._buffer - # rewrite content length self.request.headers["Content-Length"] = len(body) try: # call the configured fallback with request and body to use - self._fallback(self.request, body) - self._headers_written = True + result = self._fallback(self.request, body) + if result is not None: + await result + # self._headers_written = True finally: # make sure the temporary files are removed again - for f in self._files: - octoprint.util.silent_remove(f) + self._cleanup_files() + + def _cleanup_files(self): + """ + Removes all temporary files created by this handler. + """ + for f in self._files: + octoprint.util.silent_remove(f) # make all http methods trigger _handle_method get = _handle_method @@ -663,12 +672,21 @@ class WsgiInputContainer: The implementation logic is basically the same as ``tornado.wsgi.WSGIContainer`` but the ``__call__`` and ``environ`` methods have been adjusted to allow for an optionally supplied ``body`` argument which is then used for ``wsgi.input``. + + Additionally, some headers can be added or removed from the response by supplying ``forced_headers`` and + ``removed_headers`` arguments. ``forced_headers`` will be added to the response, ``removed_headers`` will be removed. """ def __init__( - self, wsgi_application, headers=None, forced_headers=None, removed_headers=None + self, + wsgi_application, + executor=None, + headers=None, + forced_headers=None, + removed_headers=None, ): self.wsgi_application = wsgi_application + self.executor = dummy_executor if executor is None else executor if headers is None: headers = {} @@ -682,11 +700,19 @@ class WsgiInputContainer: self.removed_headers = removed_headers def __call__(self, request, body=None): + future = tornado.concurrent.Future() + IOLoop.current().spawn_callback( + self.handle_request, request, body=body, future=future + ) + return future + + async def handle_request(self, request, body=None, future=None): """ Wraps the call against the WSGI app, deriving the WSGI environment from the supplied Tornado ``HTTPServerRequest``. :param request: the ``tornado.httpserver.HTTPServerRequest`` to derive the WSGI environment from :param body: an optional body to use as ``wsgi.input`` instead of ``request.body``, can be a string or a stream + :param future: a future to complete after the request has been handled """ data = {} @@ -697,48 +723,72 @@ class WsgiInputContainer: data["headers"] = response_headers return response.append - app_response = self.wsgi_application( - WsgiInputContainer.environ(request, body), start_response - ) try: - response.extend(app_response) + loop = IOLoop.current() + app_response = await loop.run_in_executor( + self.executor, + self.wsgi_application, + self.environ(request, body), + start_response, + ) + try: + app_response_iter = iter(app_response) + + def next_chunk(): + try: + return next(app_response_iter) + except StopIteration: + return None + + while True: + chunk = await loop.run_in_executor(self.executor, next_chunk) + if chunk is None: + break + response.append(chunk) + finally: + if hasattr(app_response, "close"): + app_response.close() body = b"".join(response) - finally: - if hasattr(app_response, "close"): - app_response.close() - if not data: - raise Exception("WSGI app did not call start_response") + if not data: + raise Exception("WSGI app did not call start_response") - status_code, reason = data["status"].split(" ", 1) - status_code = int(status_code) - headers = data["headers"] - header_set = {k.lower() for (k, v) in headers} - body = tornado.escape.utf8(body) - if status_code != 304: - if "content-length" not in header_set: - headers.append(("Content-Length", str(len(body)))) - if "content-type" not in header_set: - headers.append(("Content-Type", "text/html; charset=UTF-8")) + status_code_str, reason = data["status"].split(" ", 1) + status_code = int(status_code_str) + headers = data["headers"] + header_set = {k.lower() for (k, v) in headers} + body = tornado.escape.utf8(body) + if status_code != 304: + if "content-length" not in header_set: + headers.append(("Content-Length", str(len(body)))) + if "content-type" not in header_set: + headers.append(("Content-Type", "text/html; charset=UTF-8")) - header_set = {k.lower() for (k, v) in headers} - for header, value in self.headers.items(): - if header.lower() not in header_set: + header_set = {k.lower() for (k, v) in headers} + for header, value in self.headers.items(): + if header.lower() not in header_set: + headers.append((header, value)) + for header, value in self.forced_headers.items(): headers.append((header, value)) - for header, value in self.forced_headers.items(): - headers.append((header, value)) - headers = [ - (header, value) - for header, value in headers - if header.lower() not in self.removed_headers - ] + headers = [ + (header, value) + for header, value in headers + if header.lower() not in self.removed_headers + ] - start_line = tornado.httputil.ResponseStartLine("HTTP/1.1", status_code, reason) - header_obj = tornado.httputil.HTTPHeaders() - for key, value in headers: - header_obj.add(key, value) - request.connection.write_headers(start_line, header_obj, chunk=body) - request.connection.finish() - self._log(status_code, request) + start_line = tornado.httputil.ResponseStartLine( + "HTTP/1.1", status_code, reason + ) + header_obj = tornado.httputil.HTTPHeaders() + for key, value in headers: + header_obj.add(key, value) + assert request.connection is not None + request.connection.write_headers(start_line, header_obj, chunk=body) + request.connection.finish() + self._log(status_code, request) + + finally: + if future is not None: + future.set_result(None) @staticmethod def environ(request, body=None):