Skip to content

The agent that grows with you

fix: a build command that starts outside the update job is put in it instead of refused

Teknium24b4f90e978f
diff --git a/hermes_cli/update_custody.py b/hermes_cli/update_custody.py
index ba8a9ed5f09..60a12e42afc 100644
--- a/hermes_cli/update_custody.py
+++ b/hermes_cli/update_custody.py
@@ -520,13 +520,17 @@ _JOIN_JOB = (
"limits.basic.flags = 0x2000\n"
"if not tree or not k.SetInformationJobObject(tree, 9, ctypes.byref(limits), ctypes.sizeof(limits)):\n"
" refuse('could not create the job for the command tree: %d' % ctypes.get_last_error())\n"
- # Verify, never assume the topology: a redirecting interpreter (a Store Python alias) can
- # join while what it starts lands outside the job. Start the command suspended and run it
- # only once Windows says it is in the job (F54).
+ # Verify, never assume the topology: a packaged interpreter (Store Python) joins, but starts a
+ # program outside its package with desktop-app breakaway, so the command leaves every job
+ # that permits breakaway, as the update job does (restarted gateways break away). Start the
+ # command suspended; if Windows says it is outside the job, put it in explicitly (F80), and
+ # run it only once it is in (F54): a command the job will not take is refused, never run.
"k.IsProcessInJob.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]\n"
"p = subprocess.Popen(sys.argv[3:], stdin=subprocess.DEVNULL, creationflags=0x4)\n"
- "inside = ctypes.c_int(0)\n"
- "if not k.IsProcessInJob(ctypes.c_void_p(int(p._handle)), h, ctypes.byref(inside)) or not inside.value:\n"
+ "def in_job():\n"
+ " inside = ctypes.c_int(0)\n"
+ " return k.IsProcessInJob(ctypes.c_void_p(int(p._handle)), h, ctypes.byref(inside)) and inside.value\n"
+ "if not in_job() and not (k.AssignProcessToJobObject(h, ctypes.c_void_p(int(p._handle))) and in_job()):\n"
" p.kill()\n"
" p.wait()\n"
" refuse('the command would start outside the update job')\n"
diff --git a/tests/hermes_cli/test_update_custody.py b/tests/hermes_cli/test_update_custody.py
index ab80e50053f..7d3a5850162 100644
--- a/tests/hermes_cli/test_update_custody.py
+++ b/tests/hermes_cli/test_update_custody.py
@@ -631,10 +631,12 @@ class _FakeWinCall:
return self.answer(*args) if callable(self.answer) else self.answer
-def _run_join_launcher(monkeypatch, *, stray_polls: int):
+def _run_join_launcher(monkeypatch, *, stray_polls: int, escaped: bool = False, rebind: int = 1):
"""Execute the real ``_JOIN_JOB`` launcher source against a recording kernel32/ntdll and a
fake leader process. The command's job reports ``stray_polls`` live processes (a build
- grandchild still writing) before its tree is empty. Returns the ordered events."""
+ grandchild still writing) before its tree is empty. ``escaped``: the suspended command
+ starts outside the update job (Store Python's breakaway), and assigning it there answers
+ ``rebind``. Returns the ordered events."""
import ctypes
import types
@@ -648,11 +650,19 @@ def _run_join_launcher(monkeypatch, *, stray_polls: int):
live["left"] -= 1
return 1
+ placed = {"update job": not escaped}
+
+ def assign(job, proc):
+ if tuple(getattr(a, "value", a) for a in (job, proc)) != (11, 33):
+ return 1
+ placed["update job"] = bool(rebind)
+ return rebind
+
def in_job(proc, job, inside):
- inside._obj.value = 1
+ inside._obj.value = int(placed["update job"])
return 1
- kernel32 = {"AssignProcessToJobObject": 1, "GetCurrentProcess": 7, "CloseHandle": 1, "CreateJobObjectW": 22,
+ kernel32 = {"AssignProcessToJobObject": assign, "GetCurrentProcess": 7, "CloseHandle": 1, "CreateJobObjectW": 22,
"SetInformationJobObject": 1, "TerminateJobObject": 1, "QueryInformationJobObject": query,
"IsProcessInJob": in_job}
dlls = {"kernel32": types.SimpleNamespace(**{n: _FakeWinCall(n, events, a) for n, a in kernel32.items()}),
@@ -703,6 +713,28 @@ def test_the_windows_build_launcher_reaps_the_command_tree_before_it_returns(mon
assert events[-1] == ("exit", 3), events
+def test_a_build_command_that_starts_outside_the_update_job_is_put_in_it_before_it_runs(monkeypatch, tmp_path):
+ """F80: under Store Python the suspended node starts outside the update job (desktop-app
+ breakaway through a job that permits breakaway); refusing it made every Node build of the
+ update fail. It is assigned to the update job while suspended and runs once it is in."""
+ monkeypatch.chdir(tmp_path) # a refusal writes its report file (argv[2]) relative to here
+ events = _run_join_launcher(monkeypatch, stray_polls=0, escaped=True)
+ assert _index(events, "AssignProcessToJobObject", 11, 33) < _index(events, "NtResumeProcess"), \
+ "the command ran before it was in the update job"
+ assert events[-1] == ("exit", 3), events
+
+
+def test_a_build_command_the_update_job_will_not_take_never_runs(monkeypatch, tmp_path):
+ """F54 kept: a suspended command outside the job that Windows also refuses to assign to it
+ is killed before its first instruction and the launcher refuses."""
+ from hermes_cli.update_custody import _REFUSED_EXIT
+
+ monkeypatch.chdir(tmp_path)
+ events = _run_join_launcher(monkeypatch, stray_polls=0, escaped=True, rebind=0)
+ assert not any(e[0] == "NtResumeProcess" for e in events) and ("kill",) in events, events
+ assert events[-1] == ("exit", _REFUSED_EXIT), events
+
+
def test_the_windows_build_launcher_returns_at_once_when_nothing_was_left(monkeypatch):
"""C3 healthy control: a build whose descendants all exited with the leader costs one poll."""
events = _run_join_launcher(monkeypatch, stray_polls=0)
diff --git a/tests/hermes_cli/test_update_lock_windows_live.py b/tests/hermes_cli/test_update_lock_windows_live.py
index 7f6262b206c..de55be2cb15 100644
--- a/tests/hermes_cli/test_update_lock_windows_live.py
+++ b/tests/hermes_cli/test_update_lock_windows_live.py
@@ -345,13 +345,15 @@ def test_a_refused_job_join_never_runs_the_command(tmp_path):
-def test_a_command_that_would_start_outside_the_job_never_runs(tmp_path):
- """F54: a launcher interpreter that joins while what it starts escapes the job (a Store
- Python alias) must not run the command. Control: a job with SILENT_BREAKAWAY_OK, which the
- launcher joins but whose children start outside it."""
+def _escaping_job_launch(tmp_path: Path, *, process_limit: int = 0):
+ """Run the real ``_JOIN_JOB`` launcher in a job with SILENT_BREAKAWAY_OK: the launcher joins,
+ but the command it starts lands outside the job, as under Store Python (a packaged
+ interpreter's desktop-app breakaway through a job that permits breakaway, F54/F80).
+ ``process_limit`` caps the job's active processes so the command cannot be added to it.
+ Returns the launcher's result and the number of processes the job ever held."""
import ctypes
- from hermes_cli.update_custody import _CUSTODY_UNAVAILABLE, _JOIN_JOB, _REFUSED_EXIT
+ from hermes_cli.update_custody import _JOIN_JOB
class _Basic(ctypes.Structure): # JOBOBJECT_BASIC_LIMIT_INFORMATION
_fields_ = [("user_limits", ctypes.c_int64 * 2), ("LimitFlags", ctypes.c_uint32),
@@ -361,12 +363,19 @@ def test_a_command_that_would_start_outside_the_job_never_runs(tmp_path):
class _Extended(ctypes.Structure): # JOBOBJECT_EXTENDED_LIMIT_INFORMATION: the breakaway flags need it
_fields_ = [("basic", _Basic), ("io", ctypes.c_uint64 * 6), ("memory", ctypes.c_size_t * 4)]
+ class _Accounting(ctypes.Structure): # JOBOBJECT_BASIC_ACCOUNTING_INFORMATION
+ _fields_ = [("times", ctypes.c_int64 * 4), ("faults", ctypes.c_uint32), ("total", ctypes.c_uint32),
+ ("active", ctypes.c_uint32), ("terminated", ctypes.c_uint32)]
+
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.CreateJobObjectW.restype = ctypes.c_void_p
kernel32.SetInformationJobObject.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_ulong]
+ kernel32.QueryInformationJobObject.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_ulong,
+ ctypes.c_void_p]
job = kernel32.CreateJobObjectW(None, None)
limits = _Extended()
- limits.basic.LimitFlags = 0x1000 # JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK
+ limits.basic.LimitFlags = 0x1000 | (0x8 if process_limit else 0) # SILENT_BREAKAWAY_OK | ACTIVE_PROCESS
+ limits.basic.ActiveProcessLimit = process_limit
assert kernel32.SetInformationJobObject(job, 9, ctypes.byref(limits), ctypes.sizeof(limits)), \
ctypes.get_last_error()
os.set_handle_inheritable(job, True)
@@ -377,6 +386,27 @@ def test_a_command_that_would_start_outside_the_job_never_runs(tmp_path):
out = subprocess.run([sys.executable, "-I", "-S", str(launcher), str(job), str(tmp_path / "report.txt"),
sys.executable, "-c", "print('built')"], startupinfo=info, capture_output=True,
text=True, encoding="utf-8", errors="replace", timeout=60)
+ usage = _Accounting()
+ assert kernel32.QueryInformationJobObject(job, 1, ctypes.byref(usage), ctypes.sizeof(usage), None), \
+ ctypes.get_last_error()
+ return out, usage.total
+
+
+def test_a_command_that_starts_outside_the_job_is_put_in_it_and_runs(tmp_path):
+ """F80: Store Python's launcher starts the managed node outside the update job; refusing it
+ failed every Node build of a native Store-Python update. The suspended command is assigned to
+ the job and runs: the build completes and the job held both the launcher and the command."""
+ out, total = _escaping_job_launch(tmp_path)
+ assert out.returncode == 0 and "built" in out.stdout, out
+ assert total >= 2, "the command ran outside the update job"
+
+
+def test_a_command_the_job_will_not_take_never_runs(tmp_path):
+ """F54 kept: a command that starts outside the job and that Windows will not add to it (here
+ the job's one-process limit, held by the launcher) is never run."""
+ from hermes_cli.update_custody import _CUSTODY_UNAVAILABLE, _REFUSED_EXIT
+
+ out, _total = _escaping_job_launch(tmp_path, process_limit=1)
assert out.returncode == _REFUSED_EXIT, out
assert "built" not in out.stdout and _CUSTODY_UNAVAILABLE in out.stderr, out