shell_command: ignore utf8 decode errors

Also catch a potential KeyError when removing running commands.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-07-06 07:26:09 -04:00
parent 5c05afae42
commit 4836f52a5d
1 changed files with 11 additions and 5 deletions

View File

@ -70,7 +70,9 @@ class SCProcess(asyncio.subprocess.Process):
if not output: if not output:
break break
if fd == 2 and self.log_stderr: if fd == 2 and self.log_stderr:
logging.info(f"{self.program_name}: {output.decode()}") logging.info(
f"{self.program_name}: "
f"{output.decode(errors='ignore')}")
output = output.rstrip(b'\n') output = output.rstrip(b'\n')
if output and cb is not None: if output and cb is not None:
cb(output) cb(output)
@ -222,14 +224,15 @@ class ShellCommand:
complete = not self.cancelled complete = not self.cancelled
if self.log_stderr and stderr: if self.log_stderr and stderr:
logging.info( logging.info(
f"{self.command[0]}: {stderr.decode()}") f"{self.command[0]}: "
f"{stderr.decode(errors='ignore')}")
if self._check_proc_success(complete, log_complete): if self._check_proc_success(complete, log_complete):
self.factory.remove_running_command(self) self.factory.remove_running_command(self)
return stdout.decode().rstrip("\n") return stdout.decode(errors='ignore').rstrip("\n")
if stdout: if stdout:
logging.debug( logging.debug(
f"Shell command '{self.name}' output:" f"Shell command '{self.name}' output:"
f"\n{stdout.decode()}") f"\n{stdout.decode(errors='ignore')}")
if self.cancelled and not timed_out: if self.cancelled and not timed_out:
break break
retries -= 1 retries -= 1
@ -292,7 +295,10 @@ class ShellCommandFactory:
self.running_commands.add(cmd) self.running_commands.add(cmd)
def remove_running_command(self, cmd: ShellCommand) -> None: def remove_running_command(self, cmd: ShellCommand) -> None:
self.running_commands.remove(cmd) try:
self.running_commands.remove(cmd)
except KeyError:
pass
def build_shell_command(self, def build_shell_command(self,
cmd: str, cmd: str,