webhooks: process request handlers using a Reactor Callback

This prevents _process_data from blocking and becoming reentrant.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-07-06 05:50:16 -04:00 committed by KevinOConnor
parent 65ade083b2
commit bfbdc04c02
1 changed files with 7 additions and 17 deletions

View File

@ -98,7 +98,6 @@ class ServerConnection:
# Klippy Connection # Klippy Connection
self.fd = self.fd_handle = self.mutex = None self.fd = self.fd_handle = self.mutex = None
self.is_server_connected = False self.is_server_connected = False
self.is_busy = False
self.partial_data = "" self.partial_data = ""
is_fileoutput = (printer.get_start_args().get('debugoutput') is_fileoutput = (printer.get_start_args().get('debugoutput')
is not None) is not None)
@ -151,29 +150,20 @@ class ServerConnection:
requests = data.split('\x03') requests = data.split('\x03')
requests[0] = self.partial_data + requests[0] requests[0] = self.partial_data + requests[0]
self.partial_data = requests.pop() 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: for req in requests:
logging.debug( logging.debug(
"ServerConnection: Request received from Moonraker %s" % (req)) "ServerConnection: Request received: %s" % (req))
try: try:
decoded_req = json_loads_byteified(req) web_request = WebRequest(json_loads_byteified(req))
self._process_request(decoded_req)
except Exception: except Exception:
logging.exception( logging.exception(
"ServerConnection: Error processing Server Request %s" "ServerConnection: Error decoding Server Request %s"
% (req)) % (req))
self.is_busy = False continue
if self.fd_handle is None: self.reactor.register_callback(
self.fd_handle = self.reactor.register_fd( lambda e, s=self: s._process_request(web_request))
self.fd, self.process_received)
def _process_request(self, req): def _process_request(self, web_request):
web_request = WebRequest(req)
try: try:
func = self.webhooks.get_callback( func = self.webhooks.get_callback(
web_request.get_path()) web_request.get_path())