shell_command: Use a lock to prevent re-entrant calls

Re-entrant calls to "run" and "run_with_response" would poison the process state resulting in unexpected behavior.  Use a lock to prevent this.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-05-21 08:18:34 -04:00
parent e7193dbb5f
commit f1ba8e3d9b
1 changed files with 65 additions and 60 deletions

View File

@ -11,6 +11,7 @@ import logging
import signal
import asyncio
from tornado import gen
from tornado.locks import Lock
from utils import ServerError
# Annotation imports
@ -146,6 +147,7 @@ class ShellCommand:
self.proc: Optional[SCProcess] = None
self.cancelled = False
self.return_code: Optional[int] = None
self.run_lock = Lock()
async def cancel(self, sig_idx: int = 1) -> None:
self.cancelled = True
@ -165,6 +167,7 @@ class ShellCommand:
log_complete: bool = True,
sig_idx: int = 1
) -> bool:
async with self.run_lock:
self.factory.add_running_command(self)
self._reset_command_data()
if not timeout:
@ -198,6 +201,7 @@ class ShellCommand:
log_complete: bool = True,
sig_idx: int = 1
) -> str:
async with self.run_lock:
self.factory.add_running_command(self)
retries = max(1, retries)
while retries > 0:
@ -217,7 +221,8 @@ class ShellCommand:
else:
complete = not self.cancelled
if self.log_stderr and stderr:
logging.info(f"{self.command[0]}: {stderr.decode()}")
logging.info(
f"{self.command[0]}: {stderr.decode()}")
if self._check_proc_success(complete, log_complete):
self.factory.remove_running_command(self)
return stdout.decode().rstrip("\n")