Fix TeePopen.terminate by moving send_signal there

This commit is contained in:
Mikhail f. Shiryaev 2024-12-04 13:08:58 +01:00
parent df3e4cbfe6
commit 62f2eda0f1
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4

View File

@ -43,25 +43,24 @@ class TeePopen:
self.process.pid,
self.timeout,
)
self.send_signal(signal.SIGTERM)
self.timeout_exceeded = True
self.terminate()
def terminate(self) -> None:
def terminate(self, wait_before_kill: int = 100) -> None:
time_wait = 0
time_sleep = 5
self.terminated_by_sigterm = True
while self.process.poll() is None and time_wait < 100:
print("wait...")
wait = 5
sleep(wait)
time_wait += wait
self.send_signal(signal.SIGTERM)
while self.process.poll() is None and time_wait < wait_before_kill:
logging.warning("Wait the process %s to terminate", self.process.pid)
sleep(time_sleep)
time_wait += time_sleep
self.terminated_by_sigkill = True
while self.process.poll() is None:
logging.error(
"Process is still running. Send SIGKILL",
)
logging.error("Process is still running. Send SIGKILL")
self.send_signal(signal.SIGKILL)
self.terminated_by_sigkill = True
sleep(5)
sleep(time_sleep)
def __enter__(self) -> "TeePopen":
self.process = Popen(