feat(shell): add timeout warning and faster update

see #990
This commit is contained in:
tobi-wan-kenobi 2023-09-15 15:15:31 +02:00
parent d303b794f3
commit 2e7e75a27c
2 changed files with 15 additions and 1 deletions

View file

@ -61,6 +61,7 @@ class Module(core.module.Module):
# if requested then run not async version and just execute command in this thread # if requested then run not async version and just execute command in this thread
if not self.__async: if not self.__async:
self.__output = util.cli.execute(self.__command, shell=True, ignore_errors=True).strip() self.__output = util.cli.execute(self.__command, shell=True, ignore_errors=True).strip()
core.event.trigger("update", [self.id], redraw_only=True)
return return
# if previous thread didn't end yet then don't do anything # if previous thread didn't end yet then don't do anything
@ -71,6 +72,7 @@ class Module(core.module.Module):
self.__current_thread = threading.Thread( self.__current_thread = threading.Thread(
target=lambda obj, cmd: obj.set_output( target=lambda obj, cmd: obj.set_output(
util.cli.execute(cmd, ignore_errors=True) util.cli.execute(cmd, ignore_errors=True)
core.event.trigger("update", [self.id], redraw_only=True)
), ),
args=(self, self.__command), args=(self, self.__command),
) )

View file

@ -52,6 +52,18 @@ def execute(
raise RuntimeError("{} not found".format(cmd)) raise RuntimeError("{} not found".format(cmd))
if wait: if wait:
timeout = 60
try:
out, _ = proc.communicate(timeout=timeout)
except subprocess.TimeoutExpired as e:
logging.warning(
f"""
Communication with process pid={proc.pid} hangs for more
than {timeout} seconds.
If this is not expected, the process is stale, or
you might have run in stdout / stderr deadlock.
"""
)
out, _ = proc.communicate() out, _ = proc.communicate()
if proc.returncode != 0: if proc.returncode != 0:
err = "{} exited with code {}".format(cmd, proc.returncode) err = "{} exited with code {}".format(cmd, proc.returncode)