Skip to content

Commit

Permalink
fix: pid check
Browse files Browse the repository at this point in the history
  • Loading branch information
phi-friday committed Mar 2, 2024
1 parent 93d4e42 commit 7ffff81
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/timeout_executor/terminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from itertools import chain
from typing import Callable, Iterable

from psutil import pid_exists
from typing_extensions import Self, override

from timeout_executor.logging import logger
Expand Down Expand Up @@ -110,9 +111,24 @@ def close(self, name: str | None = None) -> None:
logger.debug("%r try to terminate process from %s", self, name or "unknown")
process = self.callback_args.process
if process.returncode is None:
with suppress(ProcessLookupError):
process.terminate()
self.is_active = True
if pid_exists(process.pid):
try:
process.terminate()
except ProcessLookupError:
logger.warning(
"%r process has no return code "
"but cant find process :: pid: %d",
self,
process.pid,
)
else:
self.is_active = True
else:
logger.warning(
"%r process has no return code but cant find process :: pid: %d",
self,
process.pid,
)

if process.stdout is not None:
text = process.stdout.read()
Expand Down

0 comments on commit 7ffff81

Please sign in to comment.