fix(exec): clean up failed job assignment

This commit is contained in:
chengyongru
2026-08-12 14:37:27 +08:00
committed by chengyongru
parent bcf5d8a6ed
commit a6193932a0
3 changed files with 41 additions and 22 deletions
+27 -4
View File
@@ -8,7 +8,7 @@ platform-specific binaries (all subprocess calls are mocked).
import asyncio
import shutil
import sys
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -27,9 +27,6 @@ class _FakeWindowsJob:
def assign_and_resume(self, pid: int) -> None:
pass
def close(self) -> None:
pass
def release(self) -> None:
pass
@@ -149,6 +146,32 @@ class TestSpawnUnix:
class TestSpawnWindows:
@pytest.mark.asyncio
async def test_job_assignment_failure_kills_suspended_process(self):
env = {"PATH": ""}
process = AsyncMock()
process.pid = 123
process.returncode = None
process.kill = MagicMock()
process.wait.return_value = -9
job = MagicMock(spec=_FakeWindowsJob)
job.creation_flags = 0x4
job.assign_and_resume.side_effect = OSError("OpenProcess failed")
with (
patch("nanobot.agent.tools.shell._IS_WINDOWS", True),
patch("nanobot.agent.tools.shell.sys.platform", "win32"),
patch("asyncio.create_subprocess_exec", new_callable=AsyncMock) as mock_exec,
patch.object(ExecTool, "_create_windows_job", return_value=job),
pytest.raises(OSError, match="OpenProcess failed"),
):
mock_exec.return_value = process
await ExecTool._spawn("echo hi", r"C:\work", env, process_tree=True)
job.terminate.assert_called_once_with()
process.kill.assert_called_once_with()
process.wait.assert_awaited_once_with()
@pytest.mark.asyncio
async def test_single_line_uses_powershell(self):
"""Single-line commands on Windows now route through PowerShell."""