Skip to content

Commit

Permalink
Require process creation time.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Oct 14, 2024
1 parent e17a61e commit c440966
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions testing/devpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pex.common import safe_open, safe_rmtree
from pex.interpreter import PythonInterpreter
from pex.interpreter_constraints import InterpreterConstraint
from pex.typing import TYPE_CHECKING
from pex.typing import TYPE_CHECKING, cast
from pex.venv.virtualenv import InvalidVirtualenvError, Virtualenv
from testing import PEX_TEST_DEV_ROOT

Expand Down Expand Up @@ -46,7 +46,7 @@ def load(cls):
try:
with open(cls._PATH) as fp:
data = json.load(fp)
return cls(url=data["url"], pid=data["pid"], create_time=data.get("create_time"))
return cls(url=data["url"], pid=data["pid"], create_time=data["create_time"])
except (OSError, IOError, ValueError, KeyError) as e:
logger.warning(
"Failed to load devpi-server pid file from {path}: {err}".format(
Expand All @@ -71,6 +71,14 @@ def _read_base_url(
return match.group("url")
return None

@staticmethod
def _get_process_creation_time(pid):
# type: (int) -> Optional[float]
try:
return cast(float, psutil.Process(pid).create_time())
except psutil.Error:
return None

@classmethod
def record(
cls,
Expand All @@ -83,9 +91,8 @@ def record(
if not base_url:
return None

try:
create_time = psutil.Process(pid).create_time()
except psutil.Error:
create_time = cls._get_process_creation_time(pid)
if create_time is None:
return None

url = "{base_url}/root/pypi/+simple/".format(base_url=base_url)
Expand All @@ -95,15 +102,11 @@ def record(

url = attr.ib() # type: str
pid = attr.ib() # type: int
create_time = attr.ib() # type: Optional[float]
create_time = attr.ib() # type: float

def alive(self):
# type: () -> bool
try:
process = psutil.Process(self.pid)
return self.create_time is None or self.create_time == process.create_time()
except psutil.NoSuchProcess:
return False
return self.create_time == self._get_process_creation_time(self.pid)

def kill(self):
# type: () -> None
Expand Down

0 comments on commit c440966

Please sign in to comment.