More resilient termination logic for CommandStream.

This commit is contained in:
Fabio Manganiello 2023-09-04 02:47:18 +02:00
parent c69f97c0a5
commit b3c82fe0d1
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,4 +1,4 @@
from multiprocessing import Queue from multiprocessing import RLock, Queue
import os import os
from queue import Empty from queue import Empty
import socket import socket
@ -35,6 +35,7 @@ class CommandStream(ControllableProcess):
self.path = os.path.abspath(os.path.expanduser(path or self._default_sock_path)) self.path = os.path.abspath(os.path.expanduser(path or self._default_sock_path))
self._sock: Optional[socket.socket] = None self._sock: Optional[socket.socket] = None
self._cmd_queue: Queue["Command"] = Queue() self._cmd_queue: Queue["Command"] = Queue()
self._close_lock = RLock()
def reset(self): def reset(self):
if self._sock is not None: if self._sock is not None:
@ -68,9 +69,18 @@ class CommandStream(ControllableProcess):
return self return self
def __exit__(self, *_, **__): def __exit__(self, *_, **__):
with self._close_lock:
self.terminate() self.terminate()
self.join()
try:
self.close() self.close()
except Exception as e:
self.logger.warning(str(e))
try:
self.kill()
except Exception as e:
self.logger.warning(str(e))
def _serve(self, sock: socket.socket): def _serve(self, sock: socket.socket):
""" """