From bfbdc04c02fb3c4b58e38e6d534024a4987e9aff Mon Sep 17 00:00:00 2001 From: Arksine Date: Mon, 6 Jul 2020 05:50:16 -0400 Subject: [PATCH] webhooks: process request handlers using a Reactor Callback This prevents _process_data from blocking and becoming reentrant. Signed-off-by: Eric Callahan --- klippy/webhooks.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/klippy/webhooks.py b/klippy/webhooks.py index 0379e3ab..1decdce5 100644 --- a/klippy/webhooks.py +++ b/klippy/webhooks.py @@ -98,7 +98,6 @@ class ServerConnection: # Klippy Connection self.fd = self.fd_handle = self.mutex = None self.is_server_connected = False - self.is_busy = False self.partial_data = "" is_fileoutput = (printer.get_start_args().get('debugoutput') is not None) @@ -151,29 +150,20 @@ class ServerConnection: requests = data.split('\x03') requests[0] = self.partial_data + requests[0] self.partial_data = requests.pop() - if self.is_busy: - if len(requests > 20): - self.reactor.unregister_fd(self.fd_handle) - self.fd_handle = None - return - self.is_busy = True for req in requests: logging.debug( - "ServerConnection: Request received from Moonraker %s" % (req)) + "ServerConnection: Request received: %s" % (req)) try: - decoded_req = json_loads_byteified(req) - self._process_request(decoded_req) + web_request = WebRequest(json_loads_byteified(req)) except Exception: logging.exception( - "ServerConnection: Error processing Server Request %s" + "ServerConnection: Error decoding Server Request %s" % (req)) - self.is_busy = False - if self.fd_handle is None: - self.fd_handle = self.reactor.register_fd( - self.fd, self.process_received) + continue + self.reactor.register_callback( + lambda e, s=self: s._process_request(web_request)) - def _process_request(self, req): - web_request = WebRequest(req) + def _process_request(self, web_request): try: func = self.webhooks.get_callback( web_request.get_path())