Answer stability round 1: 1 🔴, 4 🟡 (1 misread); keep a shell whose command holds a heavy slot, and say a failed record write on stderr

This commit is contained in:
2026-11-18 20:12:40 +01:00
parent abae3a0559
commit a7439c2e00
6 changed files with 108 additions and 66 deletions
+48 -33
View File
@@ -139,45 +139,60 @@ class PythonEffect(unittest.TestCase):
class ChildShells(unittest.TestCase):
def wait_until(self, condition):
"""Polls up to 5 s for condition, so a test doesn't depend on how fast a process starts."""
for _ in range(100):
if condition():
return
time.sleep(0.05)
self.fail("a process did not reach its state in time")
def started(self, command, **options):
"""A shell running command, once a process runs below it."""
proc = subprocess.Popen(["bash", "-c", command], **options)
self.addCleanup(lambda: (proc.kill(), proc.wait()))
self.wait_until(lambda: child_shells.descendants(proc.pid))
return proc
def shell_of(self, proc):
[found] = [each for each in child_shells.shells_of(os.getpid()) if each.pid == proc.pid]
return found
def test_reads_a_child_shells_command_and_whether_a_busy_tool_runs_below_it(self):
with tempfile.TemporaryDirectory() as folder:
tool = pathlib.Path(folder, "chunk-pr")
tool.write_text("#!/bin/sh\nsleep 60\n")
tool.chmod(0o755)
# Like the shell of a loop that only names the tool, as the one that held a worker on 2026-11-18 did.
naming = subprocess.Popen(["bash", "-c", "source /dev/null && eval 'until false; do sleep 60; done # chunk-pr merge'"])
busy = subprocess.Popen(["bash", "-c", f"{tool} merge; true"])
other = subprocess.Popen(["sleep", "60"])
try:
time.sleep(0.5)
shells = {shell.pid: shell for shell in child_shells.shells_of(os.getpid())}
self.assertEqual(sorted(shells), sorted([naming.pid, busy.pid]))
self.assertEqual(shells[naming.pid].command, "until false; do sleep 60; done # chunk-pr merge")
self.assertEqual((shells[naming.pid].runs_busy_tool, shells[busy.pid].runs_busy_tool), (False, True))
self.assertLess(abs(shells[naming.pid].started - time.time()), 5)
finally:
for proc in (naming, busy, other):
proc.kill()
proc.wait()
naming = self.started("source /dev/null && eval 'until false; do sleep 60; done # chunk-pr merge' < /dev/null && pwd")
busy = self.started(f"{tool} merge; true")
self.assertEqual(self.shell_of(naming).command, "until false; do sleep 60; done # chunk-pr merge")
self.assertEqual((self.shell_of(naming).runs_busy_tool, self.shell_of(busy).runs_busy_tool), (False, True))
self.assertLess(abs(self.shell_of(naming).started - time.time()), 5)
def test_a_shell_below_which_a_process_holds_a_heavy_slot_file_runs_a_busy_tool(self):
# run-heavy replaces itself with its command, which keeps the slot's file open.
with tempfile.TemporaryDirectory() as state, unittest.mock.patch.dict(os.environ, {"XDG_STATE_HOME": state}):
slot = pathlib.Path(state, "run-heavy", "slot-0")
slot.parent.mkdir()
holding = self.started(f"exec 9>{slot}; sleep 60; true")
plain = self.started("sleep 60; true")
self.assertEqual((self.shell_of(holding).runs_busy_tool, self.shell_of(plain).runs_busy_tool), (True, False))
def test_a_shell_written_output_is_the_modified_time_of_its_output_file_and_else_its_start(self):
with tempfile.NamedTemporaryFile() as output:
os.utime(output.name, (1000, 1000))
to_file = self.started("sleep 60; true", stdout=output)
to_pipe = self.started("sleep 60; true", stdout=subprocess.PIPE)
self.assertEqual(self.shell_of(to_file).written, 1000)
self.assertEqual(self.shell_of(to_pipe).written, self.shell_of(to_pipe).started)
def test_stops_a_shell_and_the_process_it_started_and_no_other(self):
shell = subprocess.Popen(["bash", "-c", "sleep 60; true"])
shell = self.started("sleep 60; true")
other = subprocess.Popen(["sleep", "60"])
try:
time.sleep(0.5)
found = child_shells.shells_of(os.getpid())
[stopping] = [each for each in found if each.pid == shell.pid]
below = child_shells.descendants(shell.pid)
self.assertEqual(len(below), 1)
child_shells.stop(stopping)
self.assertEqual(shell.wait(timeout=5), -signal.SIGTERM)
for _ in range(50):
if not os.path.exists(f"/proc/{below[0]}"):
break
time.sleep(0.1)
self.assertFalse(os.path.exists(f"/proc/{below[0]}"))
self.assertIsNone(other.poll())
finally:
for proc in (shell, other):
proc.kill()
proc.wait()
self.addCleanup(lambda: (other.kill(), other.wait()))
[below] = child_shells.descendants(shell.pid)
child_shells.stop(self.shell_of(shell))
self.assertEqual(shell.wait(timeout=5), -signal.SIGTERM)
self.wait_until(lambda: not os.path.exists(f"/proc/{below}"))
self.assertIsNone(other.poll())