Spaces:
Sleeping
Sleeping
Download src/tests/runtime/test_process_lifecycle.py from dellweb3/VibeCoding: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_process_lifecycle.py
- Command line
-
hf download hf://spaces/dellweb3/VibeCoding/src/tests/runtime/test_process_lifecycle.py
-
curl -L -o test_process_lifecycle.py https://huggingface.co/spaces/dellweb3/VibeCoding/resolve/main/src/tests/runtime/test_process_lifecycle.py
3.24 kB
| """Actual platform child processes; no generated code is executed on the host.""" | |
| import os | |
| import subprocess | |
| import sys | |
| import tempfile | |
| import threading | |
| import time | |
| import unittest | |
| from pathlib import Path | |
| from src.adapters.sandbox.docker import CommandRunner | |
| from src.adapters.sandbox.process_lifecycle import spawn_owned, release_owned | |
| from src.modules.skill.domain.models import SkillError | |
| class ProcessLifecycleTests(unittest.TestCase): | |
| def test_normal_exit_and_job_release(self): | |
| child = spawn_owned([sys.executable, '-c', 'print("owned")'], stdout=subprocess.PIPE) | |
| self.assertEqual(child.communicate(timeout=10)[0].strip(), b'owned') | |
| release_owned(child) | |
| release_owned(child) | |
| def test_runner_close_reaps_active_child_and_rejects_new_work(self): | |
| runner = CommandRunner() | |
| results = [] | |
| def run(): | |
| try: runner([sys.executable, '-c', 'import time; time.sleep(60)'], timeout=90) | |
| except SkillError as exc: results.append(exc.code) | |
| thread = threading.Thread(target=run) | |
| thread.start() | |
| try: | |
| deadline = time.monotonic() + 10 | |
| while not runner._processes and time.monotonic() < deadline: time.sleep(.02) | |
| self.assertTrue(runner._processes) | |
| children = list(runner._processes) | |
| runner.close() | |
| thread.join(timeout=10) | |
| self.assertFalse(thread.is_alive()) | |
| self.assertTrue(all(child.poll() is not None for child in children)) | |
| with self.assertRaises(SkillError): runner([sys.executable, '-c', 'pass'], timeout=10) | |
| finally: | |
| runner.close() | |
| thread.join(timeout=10) | |
| def test_abrupt_parent_exit_kills_only_owned_child(self): | |
| import ctypes | |
| from ctypes import wintypes | |
| kernel = ctypes.WinDLL('kernel32', use_last_error=True) | |
| kernel.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD] | |
| kernel.OpenProcess.restype = wintypes.HANDLE | |
| kernel.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD] | |
| kernel.CloseHandle.argtypes = [wintypes.HANDLE] | |
| parent = subprocess.Popen([sys.executable, '-c', | |
| 'import sys,time; from src.adapters.sandbox.process_lifecycle import spawn_owned; ' | |
| 'p=spawn_owned([sys.executable,"-c","import time; time.sleep(60)"]); ' | |
| 'print(p.pid,flush=True); time.sleep(60)'], stdout=subprocess.PIPE, text=True) | |
| handle = None | |
| unrelated = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)']) | |
| try: | |
| pid = int(parent.stdout.readline()) | |
| handle = kernel.OpenProcess(0x100000, False, pid) | |
| self.assertTrue(handle) | |
| parent.kill(); parent.wait(timeout=5) | |
| self.assertEqual(kernel.WaitForSingleObject(handle, 10000), 0) | |
| self.assertIsNone(unrelated.poll()) | |
| finally: | |
| if parent.poll() is None: parent.kill(); parent.wait(timeout=5) | |
| parent.stdout.close() | |
| if handle: kernel.CloseHandle(handle) | |
| unrelated.kill(); unrelated.wait(timeout=5) | |