A marker left by a force-killed app no longer blocks the next load when its process number belongs to a later program

The child marker without a creation time could not tell the recorded engine from another Python or Strata program that had reused the
number, so every load said "lost host child is still present" until the marker was deleted by hand. The child is always started before
its marker is written, so a live process created after the marker's file time (with a two-second slack) is not the child. Unreadable
times still keep refusing; a recorded creation time still decides by itself. New procs.started_epoch reads a creation time on Windows,
Linux and macOS.
This commit is contained in:
Niko1221 committed 2026-10-22 15:02:49 +02:00
1 parent a7cd2de97a
commit 098fd545b3
3 files changed
+89 -5

No files matched your search

+24
View File
@@ -78,6 +78,30 @@ def start_token(pid: int) -> str | None:
return None if not out or out.endswith("Z") else "s:" + out
def started_epoch(pid: int) -> float | None:
"""When the process at `pid` was created, in seconds since 1970, or None when there is no such process or the time cannot be read.
A number recorded in an old file can belong to a later process; a creation time after the file was written proves it does."""
if type(pid) is not int or pid <= 0:
return None
if psutil is not None:
try:
return float(psutil.Process(pid).create_time())
except (psutil.Error, OSError):
return None
if WINDOWS:
from strata_control import winproc
ticks = winproc.process_started(pid)
return None if ticks is None else (ticks - 116444736000000000) / 1e7 # 100 ns ticks since 1601
if sys.platform.startswith("linux"):
token = start_token(pid)
try:
boot = next(int(line.split()[1]) for line in Path("/proc/stat").read_text().splitlines() if line.startswith("btime "))
return boot + int(token[2:]) / os.sysconf("SC_CLK_TCK") if token else None
except (OSError, ValueError, StopIteration):
return None
return None
def command_line(pid: int) -> list[str] | None:
"""The process's argv, or None when it cannot be read (then it is never treated as a match)."""
if sys.platform.startswith("linux"):
+17 -4
View File
@@ -254,13 +254,25 @@ def _pid_exists(pid: int, started: int | None = None) -> bool:
return True
def _child_present(child: dict) -> bool:
_MARKER_SLACK = 2.0 # seconds: the child starts before its marker is written; this much of clock rounding is allowed
def _child_present(child: dict, written: float | None = None) -> bool:
"""Whether the child recorded in an owner marker is still there. A marker without a creation time (Windows could not
read it when the child started) cannot tell the recorded server from a later process that reused the number, so
a live process that is not a Python or Strata executable is a reused number: the child is gone (#3488)."""
a live process that is not a Python or Strata executable is a reused number: the child is gone (#3488).
`written` is when the marker was written (its file time). The child was started before that, so a process that was created
AFTER it cannot be the child, whatever its image: the number was reused. This is what clears a marker left by a force-killed app
when the number now belongs to another Python program (2026-10-22: every load said "still present", and removing the marker by
hand was the only way out)."""
started = child.get("started") if type(child.get("started")) is int else None
if not _pid_exists(child["pid"], started):
return False
if started is None and written is not None:
created = procs.started_epoch(child["pid"])
if created is not None and created > written + _MARKER_SLACK:
return False
if WINDOWS and started is None:
image = winproc.process_image(child["pid"])
if image is not None:
@@ -307,9 +319,10 @@ def _recover_stale(name: str, *, wait: float) -> bool:
child.get("phase") != "owned" or type(child.get("pid")) is not int or child["pid"] <= 0:
raise ProfileError("lost host child launch is unverified; inspect before recovery", 6)
deadline = time.monotonic() + min(45.0, max(5.0, wait))
while _child_present(child) and time.monotonic() < deadline:
written = details.st_mtime
while _child_present(child, written) and time.monotonic() < deadline:
time.sleep(0.1)
if _child_present(child):
if _child_present(child, written):
raise ProfileError("lost host child is still present; retry after it exits", 6)
if not isinstance(child.get("ready"), str):
raise ProfileError("lost host ready path is invalid; inspect before recovery", 6)
+48 -1
View File
@@ -21,7 +21,7 @@ from unittest import mock
from strata_cli.cli import main
from strata_cli import bench
from strata_cli.client import Endpoint
from strata_control import calibration, journal, management, owner_transport, profiles, recipes, runtime
from strata_control import calibration, journal, management, owner_transport, procs, profiles, recipes, runtime
WINDOWS = os.name == "nt"
@@ -1159,6 +1159,53 @@ class RuntimeTests(unittest.TestCase):
image.return_value = r"C:\Windows\System32\svchost.exe"
self.assertTrue(runtime._child_present({"pid": 1, "started": 5})) # a creation time decides by itself
def test_a_process_created_after_the_marker_was_written_is_not_the_recorded_child(self):
# 2026-10-22: the app was force-killed, its marker had no creation time, the number now belonged to another Python program, and every
# load said "lost host child is still present" until the marker was deleted by hand. The child was started BEFORE its marker was written.
child = {"pid": 4242}
with mock.patch.object(runtime, "_pid_exists", return_value=True), \
mock.patch.object(runtime.winproc, "process_image", create=True, return_value="C:\\Python\\python.exe"), \
mock.patch.object(runtime.procs, "started_epoch") as created:
created.return_value = 1000.0 + 60
self.assertFalse(runtime._child_present(child, written=1000.0), "created a minute after the marker: another process")
created.return_value = 1000.0 - 5
self.assertTrue(runtime._child_present(child, written=1000.0), "created before the marker: could be the child, keep refusing")
created.return_value = 1000.0 + 1.5
self.assertTrue(runtime._child_present(child, written=1000.0), "inside the clock slack: keep refusing")
created.return_value = None
self.assertTrue(runtime._child_present(child, written=1000.0), "creation time unreadable: never guess")
created.return_value = 1000.0 + 60
self.assertTrue(runtime._child_present({"pid": 4242, "started": 7}, written=1000.0) is True, "a recorded creation time decides by itself")
self.assertTrue(runtime._child_present(child), "no marker time: unchanged")
def test_a_marker_left_by_a_killed_app_is_cleaned_up_when_its_number_belongs_to_a_later_program(self):
victim = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(120)"])
try:
state_path, marker = self._stale_host(victim.pid) # a marker with no creation time (Windows could not read it)
old = time.time() - 3600
os.utime(marker, (old, old)) # written an hour ago: the victim started after it
started = runtime.start("local", wait=10, shutdown_timeout=2)
self.assertTrue(started["ok"], started)
self.assertFalse(marker.exists(), "the stale marker was removed")
self.assertIsNone(victim.poll(), "the program that reused the number was left alone")
finally:
victim.kill()
victim.wait(timeout=10)
def test_started_epoch_reads_a_live_process_and_nothing_for_a_dead_number(self):
victim = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(120)"])
try:
now = time.time()
created = procs.started_epoch(victim.pid)
self.assertIsNotNone(created)
self.assertLess(abs(created - now), 60)
finally:
victim.kill()
victim.wait(timeout=10)
self.assertIsNone(procs.started_epoch(victim.pid))
self.assertIsNone(procs.started_epoch(0))
self.assertIsNone(procs.started_epoch(-5))
@unittest.skipUnless(WINDOWS, "os.kill(pid, 0) ends a process on Windows; liveness is a handle query")
def test_liveness_check_does_not_end_the_process_it_asks_about(self):
victim = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(120)"])