Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle devpi-server pid rollover. #2559

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions testing/devpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import subprocess
import time

import psutil # type: ignore[import]

from pex.atomic_directory import atomic_directory
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 @@ -44,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"])
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 @@ -69,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 @@ -81,24 +91,22 @@ def record(
if not base_url:
return None

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)
with safe_open(cls._PATH, "w") as fp:
json.dump(dict(url=url, pid=pid), fp, indent=2, sort_keys=True)
return cls(url=url, pid=pid)
json.dump(dict(url=url, pid=pid, create_time=create_time), fp, indent=2, sort_keys=True)
return cls(url=url, pid=pid, create_time=create_time)

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

def alive(self):
# type: () -> bool
# TODO(John Sirois): Handle pid rollover
try:
os.kill(self.pid, 0)
return True
except OSError as e:
if e.errno == errno.ESRCH: # No such process.
return False
raise
return self.create_time == self._get_process_creation_time(self.pid)

def kill(self):
# type: () -> None
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ deps =
# Python < 3.6 in newer releases so we force low here.
more-itertools<=8.10.0; python_version < "3.6"
pexpect==4.9.0
psutil
pytest==4.6.11; python_version < "3.6"
pytest==6.2.5; python_version == "3.6"
pytest==7.4.4; python_version == "3.7"
Expand Down