fix(shell): separate wait, idle, and runtime limits (#6228)
* fix(shell): separate wait, idle, and runtime limits * fix(shell): separate wait, idle, and runtime limits * fix(exec): preserve settings aliases and reclaim completed sessions * fix(webui): make shell runtime limits explicit
This commit is contained in:
1 parent
70ac000ad9
commit
6310a39bac
36 files changed
+2392
-564
No files matched your search
+274
-121
@@ -11,7 +11,8 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.agent.tools.exec_session import _ExecSession
|
||||
from nanobot.agent.tools.exec_session import ExecSessionManager, ExecSessionTool, _ExecSession
|
||||
from nanobot.agent.tools.registry import is_tool_error_result
|
||||
from nanobot.agent.tools.shell import ExecTool, _reap_pid
|
||||
|
||||
|
||||
@@ -21,6 +22,50 @@ def _python_command(code: str) -> str:
|
||||
return f"{shlex.quote(sys.executable)} -u -c {shlex.quote(code)}"
|
||||
|
||||
|
||||
class _MockProcess:
|
||||
def __init__(
|
||||
self, *, pid: int, returncode: int | None, stdout: bytes = b"", stderr: bytes = b"",
|
||||
) -> None:
|
||||
self.pid = pid
|
||||
self.returncode = returncode
|
||||
self.stdin = None
|
||||
self.stdout = asyncio.StreamReader()
|
||||
self.stderr = asyncio.StreamReader()
|
||||
self.stdout.feed_data(stdout)
|
||||
self.stderr.feed_data(stderr)
|
||||
self.exited = asyncio.Event()
|
||||
self._exit_code = returncode
|
||||
self.kill = MagicMock(side_effect=lambda: self.exit(-9))
|
||||
self.wait = AsyncMock(side_effect=self._wait)
|
||||
if returncode is not None:
|
||||
self.exit(returncode)
|
||||
|
||||
def exit(self, returncode: int, *, close_streams: bool = True) -> None:
|
||||
self._exit_code = returncode
|
||||
if close_streams:
|
||||
self.stdout.feed_eof()
|
||||
self.stderr.feed_eof()
|
||||
self.exited.set()
|
||||
|
||||
async def _wait(self) -> int:
|
||||
await self.exited.wait()
|
||||
assert self._exit_code is not None
|
||||
self.returncode = self._exit_code
|
||||
return self.returncode
|
||||
|
||||
|
||||
async def _cleanup_session(session: _ExecSession) -> None:
|
||||
await asyncio.wait_for(session.kill(), timeout=5)
|
||||
tasks = (
|
||||
session._stdout_task, session._stderr_task, session._completion_task,
|
||||
session._timeout_task, session._kill_task,
|
||||
)
|
||||
await asyncio.wait_for(
|
||||
asyncio.gather(*(task for task in tasks if task is not None), return_exceptions=True),
|
||||
timeout=5,
|
||||
)
|
||||
|
||||
|
||||
def test_reap_pid_noops_without_waitpid():
|
||||
"""On platforms (or test stubs) without waitpid, reaping is a no-op."""
|
||||
with patch("nanobot.agent.tools.shell.os") as mock_os:
|
||||
@@ -50,9 +95,7 @@ def test_reap_pid_swallows_already_reaped_errors():
|
||||
@pytest.mark.asyncio
|
||||
async def test_kill_process_skips_kill_when_already_exited():
|
||||
"""Already-dead processes must not raise ProcessLookupError from kill()."""
|
||||
process = AsyncMock()
|
||||
process.pid = 4242
|
||||
process.returncode = 0
|
||||
process = _MockProcess(pid=4242, returncode=0)
|
||||
process.kill = MagicMock(side_effect=ProcessLookupError("already dead"))
|
||||
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
@@ -65,147 +108,249 @@ async def test_kill_process_skips_kill_when_already_exited():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_kill_process_kills_and_reaps_live_process():
|
||||
process = AsyncMock()
|
||||
process.pid = 77
|
||||
process.returncode = None
|
||||
process.kill = MagicMock()
|
||||
process.wait = AsyncMock(return_value=0)
|
||||
process = _MockProcess(pid=77, returncode=None)
|
||||
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
await ExecTool._kill_process(process)
|
||||
|
||||
process.kill.assert_called_once()
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == -9
|
||||
reap.assert_called_once_with(77)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_kill_process_reaps_even_if_kill_races_exit():
|
||||
"""If the child exits between returncode check and kill(), still reap."""
|
||||
process = AsyncMock()
|
||||
process.pid = 88
|
||||
process.returncode = None
|
||||
process.kill = MagicMock(side_effect=ProcessLookupError("raced exit"))
|
||||
process.wait = AsyncMock(return_value=0)
|
||||
process = _MockProcess(pid=88, returncode=None)
|
||||
|
||||
def raced_exit() -> None:
|
||||
process.exit(0)
|
||||
raise ProcessLookupError("raced exit")
|
||||
|
||||
process.kill.side_effect = raced_exit
|
||||
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
await ExecTool._kill_process(process)
|
||||
|
||||
process.kill.assert_called_once()
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == 0
|
||||
reap.assert_called_once_with(88)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_reaps_after_normal_completion():
|
||||
mock_proc = AsyncMock()
|
||||
mock_proc.pid = 1001
|
||||
mock_proc.communicate.return_value = (b"ok\n", b"")
|
||||
mock_proc.returncode = 0
|
||||
|
||||
process = _MockProcess(pid=1001, returncode=0, stdout=b"ok\n")
|
||||
manager = ExecSessionManager()
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=mock_proc),
|
||||
patch.object(ExecTool, "_guard_command", return_value=None),
|
||||
patch.object(ExecTool, "_spawn", return_value=process),
|
||||
patch("nanobot.agent.tools.shell._reap_pid") as reap,
|
||||
):
|
||||
tool = ExecTool()
|
||||
result = await tool.execute(command="echo ok")
|
||||
try:
|
||||
result = await ExecTool(session_manager=manager).execute(command="echo ok")
|
||||
finally:
|
||||
await manager.close_all()
|
||||
|
||||
assert not is_tool_error_result(result)
|
||||
assert "ok" in result
|
||||
assert "Exit code: 0" in result
|
||||
process.wait.assert_awaited()
|
||||
process.kill.assert_not_called()
|
||||
reap.assert_called_with(1001)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_timeout_kills_and_reaps():
|
||||
mock_proc = AsyncMock()
|
||||
mock_proc.pid = 1002
|
||||
mock_proc.returncode = None
|
||||
mock_proc.communicate.side_effect = asyncio.TimeoutError()
|
||||
|
||||
process = _MockProcess(pid=1002, returncode=None)
|
||||
manager = ExecSessionManager()
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=mock_proc) as spawn,
|
||||
patch.object(ExecTool, "_guard_command", return_value=None),
|
||||
patch.object(ExecTool, "_kill_process_tree", new_callable=AsyncMock) as kill_tree,
|
||||
patch.object(ExecTool, "_spawn", return_value=process) as spawn,
|
||||
patch.object(ExecTool, "_kill_process_tree", side_effect=ExecTool._kill_process) as kill_tree,
|
||||
patch("nanobot.agent.tools.shell._reap_pid") as reap,
|
||||
):
|
||||
tool = ExecTool(timeout=1)
|
||||
result = await tool.execute(command="sleep 99", timeout=1)
|
||||
try:
|
||||
result = await asyncio.wait_for(
|
||||
ExecTool(timeout=1, session_manager=manager).execute(command="sleep 99", timeout=1),
|
||||
timeout=5,
|
||||
)
|
||||
finally:
|
||||
await manager.close_all()
|
||||
|
||||
assert is_tool_error_result(result)
|
||||
assert "timed out" in result.lower()
|
||||
kill_tree.assert_awaited_once_with(mock_proc)
|
||||
assert "Reason: max_runtime" in result
|
||||
kill_tree.assert_awaited_once_with(process)
|
||||
process.kill.assert_called_once()
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == -9
|
||||
reap.assert_any_call(1002)
|
||||
assert spawn.await_args.kwargs["process_tree"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_cancellation_kills_process_tree():
|
||||
mock_proc = AsyncMock()
|
||||
mock_proc.pid = 1005
|
||||
mock_proc.returncode = None
|
||||
mock_proc.communicate.side_effect = asyncio.CancelledError()
|
||||
process = _MockProcess(pid=1005, returncode=None)
|
||||
manager = ExecSessionManager()
|
||||
waiting = asyncio.Event()
|
||||
wait_exit = _ExecSession.wait_exit
|
||||
|
||||
async def inline_wait(session: _ExecSession) -> None:
|
||||
waiting.set()
|
||||
await wait_exit(session)
|
||||
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=mock_proc) as spawn,
|
||||
patch.object(ExecTool, "_guard_command", return_value=None),
|
||||
patch.object(ExecTool, "_kill_process_tree", new_callable=AsyncMock) as kill_tree,
|
||||
patch.object(ExecTool, "_spawn", return_value=process) as spawn,
|
||||
patch.object(ExecTool, "_kill_process_tree", side_effect=ExecTool._kill_process) as kill_tree,
|
||||
patch.object(_ExecSession, "wait_exit", new=inline_wait),
|
||||
patch("nanobot.agent.tools.shell._reap_pid") as reap,
|
||||
):
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await ExecTool().execute(command="sleep 99")
|
||||
execution = asyncio.create_task(
|
||||
ExecTool(session_manager=manager).execute(command="sleep 99"),
|
||||
)
|
||||
try:
|
||||
await asyncio.wait_for(waiting.wait(), timeout=5)
|
||||
assert process.returncode is None
|
||||
execution.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await asyncio.wait_for(execution, timeout=5)
|
||||
assert await manager.list() == []
|
||||
finally:
|
||||
execution.cancel()
|
||||
await asyncio.gather(execution, return_exceptions=True)
|
||||
await manager.close_all()
|
||||
|
||||
kill_tree.assert_awaited_once_with(mock_proc)
|
||||
kill_tree.assert_awaited_once_with(process)
|
||||
process.kill.assert_called_once()
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == -9
|
||||
reap.assert_any_call(1005)
|
||||
assert spawn.await_args.kwargs["process_tree"] is True
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_exception_after_success_does_not_raise_on_dead_process():
|
||||
"""Generic except must return ToolResult.error, not ProcessLookupError."""
|
||||
mock_proc = AsyncMock()
|
||||
mock_proc.pid = 1003
|
||||
mock_proc.communicate = AsyncMock(return_value=(b"out\n", b""))
|
||||
mock_proc.returncode = 0
|
||||
mock_proc.kill = MagicMock(side_effect=ProcessLookupError("already dead"))
|
||||
|
||||
async def test_exec_session_wait_cancellation_does_not_kill_process_tree():
|
||||
process = _MockProcess(pid=1007, returncode=None)
|
||||
manager = ExecSessionManager()
|
||||
session_tool = ExecSessionTool(manager=manager)
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=mock_proc),
|
||||
patch.object(ExecTool, "_guard_command", return_value=None),
|
||||
patch.object(ExecTool, "_spawn", return_value=process),
|
||||
patch.object(ExecTool, "_kill_process_tree", side_effect=ExecTool._kill_process) as kill_tree,
|
||||
patch("nanobot.agent.tools.shell._reap_pid") as reap,
|
||||
patch(
|
||||
"nanobot.agent.tools.shell.clamp_session_int",
|
||||
side_effect=RuntimeError("boom after exit"),
|
||||
),
|
||||
):
|
||||
tool = ExecTool()
|
||||
result = await tool.execute(command="echo out")
|
||||
observer = None
|
||||
try:
|
||||
initial = await ExecTool(session_manager=manager).execute(
|
||||
command="sleep 99", yield_time_ms=0,
|
||||
)
|
||||
session_id, session = next(iter(manager._sessions.items()))
|
||||
assert session_id in initial
|
||||
observer = asyncio.create_task(session_tool.execute(
|
||||
session_id=session_id, until_exit=True, timeout_ms=30_000,
|
||||
))
|
||||
await asyncio.sleep(0)
|
||||
assert not observer.done()
|
||||
observer.cancel()
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
await observer
|
||||
|
||||
assert "Error executing command" in result
|
||||
assert "boom after exit" in result
|
||||
mock_proc.kill.assert_not_called()
|
||||
assert reap.called
|
||||
assert process.returncode is None
|
||||
assert not session._completion_task.done()
|
||||
process.kill.assert_not_called()
|
||||
kill_tree.assert_not_awaited()
|
||||
assert [info.session_id for info in await manager.list()] == [session_id]
|
||||
|
||||
result = await session_tool.execute(session_id=session_id, terminate=True)
|
||||
assert "Reason: cancelled" in result
|
||||
assert "Exit code: -9" in result
|
||||
finally:
|
||||
if observer is not None:
|
||||
observer.cancel()
|
||||
await asyncio.gather(observer, return_exceptions=True)
|
||||
await manager.close_all()
|
||||
|
||||
kill_tree.assert_awaited_once_with(process)
|
||||
process.kill.assert_called_once()
|
||||
reap.assert_any_call(1007)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_exception_during_communicate_kills_live_process():
|
||||
mock_proc = AsyncMock()
|
||||
mock_proc.pid = 1004
|
||||
mock_proc.returncode = None
|
||||
mock_proc.communicate.side_effect = OSError("pipe broken")
|
||||
async def test_execute_exception_after_success_does_not_raise_on_dead_process():
|
||||
"""Formatting a completed result must not attempt to kill the dead process."""
|
||||
process = _MockProcess(pid=1003, returncode=0, stdout=b"out\n")
|
||||
process.kill.side_effect = ProcessLookupError("already dead")
|
||||
manager = ExecSessionManager()
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=process),
|
||||
patch("nanobot.agent.tools.shell._reap_pid") as reap,
|
||||
patch(
|
||||
"nanobot.agent.tools.shell.format_session_poll",
|
||||
side_effect=RuntimeError("boom after exit"),
|
||||
) as formatter,
|
||||
):
|
||||
try:
|
||||
result = await ExecTool(session_manager=manager).execute(command="echo out")
|
||||
finally:
|
||||
await manager.close_all()
|
||||
|
||||
assert is_tool_error_result(result)
|
||||
assert "Error executing command" in result
|
||||
assert "boom after exit" in result
|
||||
poll = formatter.call_args.args[1]
|
||||
assert poll.done is True
|
||||
assert poll.exit_code == 0
|
||||
assert poll.output == "out\n"
|
||||
process.wait.assert_awaited()
|
||||
process.kill.assert_not_called()
|
||||
reap.assert_called_with(1003)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_execute_stream_read_error_kills_live_process(tmp_path):
|
||||
manager = ExecSessionManager()
|
||||
tool = ExecTool(working_dir=str(tmp_path), session_manager=manager)
|
||||
spawn_process = ExecTool._spawn
|
||||
kill_process_tree = ExecTool._kill_process_tree
|
||||
process = None
|
||||
|
||||
async def spawn_with_broken_stdout(*args, **kwargs):
|
||||
nonlocal process
|
||||
process = await spawn_process(*args, **kwargs)
|
||||
assert process.returncode is None
|
||||
assert process.stdout is not None
|
||||
process.stdout.set_exception(OSError("pipe broken"))
|
||||
return process
|
||||
|
||||
with (
|
||||
patch.object(ExecTool, "_spawn", return_value=mock_proc),
|
||||
patch.object(ExecTool, "_guard_command", return_value=None),
|
||||
patch.object(ExecTool, "_kill_process_tree", new_callable=AsyncMock) as kill_tree,
|
||||
patch.object(ExecTool, "_spawn", side_effect=spawn_with_broken_stdout),
|
||||
patch.object(ExecTool, "_kill_process_tree", wraps=kill_process_tree) as kill_tree,
|
||||
patch("nanobot.agent.tools.shell._reap_pid", wraps=_reap_pid) as reap,
|
||||
):
|
||||
tool = ExecTool()
|
||||
result = await tool.execute(command="broken")
|
||||
try:
|
||||
result = await asyncio.wait_for(tool.execute(
|
||||
command=[sys.executable, "-u", "-c", "import time; time.sleep(30)"],
|
||||
), timeout=10)
|
||||
finally:
|
||||
try:
|
||||
await manager.close_all()
|
||||
finally:
|
||||
if process is not None and process.returncode is None:
|
||||
await kill_process_tree(process)
|
||||
|
||||
assert "Error executing command" in result
|
||||
assert is_tool_error_result(result)
|
||||
assert "pipe broken" in result
|
||||
kill_tree.assert_awaited_once_with(mock_proc)
|
||||
assert "Reason: execution_error" in result
|
||||
assert "output may be incomplete" in result
|
||||
assert "Process running" not in result
|
||||
assert process is not None
|
||||
assert process.returncode is not None and process.returncode != 0
|
||||
kill_tree.assert_awaited_once_with(process)
|
||||
reap.assert_any_call(process.pid)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_kill_process_tree_targets_group_after_root_exits():
|
||||
process = AsyncMock()
|
||||
process.pid = 1006
|
||||
process.returncode = 0
|
||||
process = _MockProcess(pid=1006, returncode=0)
|
||||
|
||||
with (
|
||||
patch("nanobot.agent.tools.shell._IS_WINDOWS", False),
|
||||
@@ -285,22 +430,9 @@ async def test_kill_process_tree_kills_descendant_after_root_exits(tmp_path):
|
||||
await ExecTool._kill_process_tree(process)
|
||||
|
||||
|
||||
def _mock_session_process(*, pid: int, returncode: int | None):
|
||||
process = AsyncMock()
|
||||
process.pid = pid
|
||||
process.returncode = returncode
|
||||
process.kill = MagicMock()
|
||||
process.wait = AsyncMock(return_value=returncode if returncode is not None else -9)
|
||||
# Constructor starts stream readers; closed streams exit immediately.
|
||||
process.stdout = None
|
||||
process.stderr = None
|
||||
process.stdin = None
|
||||
return process
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exec_session_kill_reaps():
|
||||
process = _mock_session_process(pid=2001, returncode=None)
|
||||
process = _MockProcess(pid=2001, returncode=None)
|
||||
session = _ExecSession(
|
||||
session_id="abc",
|
||||
process=process,
|
||||
@@ -313,15 +445,24 @@ async def test_exec_session_kill_reaps():
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
await session.kill()
|
||||
process.kill.assert_called_once()
|
||||
reap.assert_called_once_with(2001)
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == -9
|
||||
assert session.finish_reason == "cancelled"
|
||||
assert session.finished_at is not None
|
||||
reap.assert_any_call(2001)
|
||||
finally:
|
||||
await asyncio.gather(session._stdout_task, session._stderr_task, return_exceptions=True)
|
||||
await _cleanup_session(session)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exec_session_kill_reaps_if_process_exits_before_kill():
|
||||
process = _mock_session_process(pid=2002, returncode=None)
|
||||
process.kill.side_effect = ProcessLookupError("raced exit")
|
||||
process = _MockProcess(pid=2002, returncode=None)
|
||||
|
||||
def raced_exit() -> None:
|
||||
process.exit(0)
|
||||
raise ProcessLookupError("raced exit")
|
||||
|
||||
process.kill.side_effect = raced_exit
|
||||
session = _ExecSession(
|
||||
session_id="raced",
|
||||
process=process,
|
||||
@@ -333,14 +474,20 @@ async def test_exec_session_kill_reaps_if_process_exits_before_kill():
|
||||
try:
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
await session.kill()
|
||||
reap.assert_called_once_with(2002)
|
||||
process.kill.assert_called_once()
|
||||
process.wait.assert_awaited()
|
||||
assert process.returncode == 0
|
||||
assert session.finished_at is not None
|
||||
reap.assert_any_call(2002)
|
||||
finally:
|
||||
await asyncio.gather(session._stdout_task, session._stderr_task, return_exceptions=True)
|
||||
await _cleanup_session(session)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exec_session_kill_waits_for_reader_tasks():
|
||||
process = _mock_session_process(pid=2003, returncode=None)
|
||||
process = _MockProcess(pid=2003, returncode=None, stdout=b"head\n")
|
||||
# OS exit can be observed before the readers have consumed the final pipe bytes.
|
||||
process.kill.side_effect = lambda: process.exit(-9, close_streams=False)
|
||||
session = _ExecSession(
|
||||
session_id="readers",
|
||||
process=process,
|
||||
@@ -349,34 +496,36 @@ async def test_exec_session_kill_waits_for_reader_tasks():
|
||||
timeout=30,
|
||||
owner_session_key=None,
|
||||
)
|
||||
await asyncio.gather(session._stdout_task, session._stderr_task)
|
||||
|
||||
release = asyncio.Event()
|
||||
|
||||
async def reader() -> None:
|
||||
await release.wait()
|
||||
|
||||
async def wait_for_process() -> int:
|
||||
release.set()
|
||||
process.returncode = -9
|
||||
return -9
|
||||
|
||||
session._stdout_task = asyncio.create_task(reader())
|
||||
session._stderr_task = asyncio.create_task(reader())
|
||||
process.wait = AsyncMock(side_effect=wait_for_process)
|
||||
stopping = asyncio.create_task(session.kill())
|
||||
try:
|
||||
await session.kill()
|
||||
await asyncio.wait_for(process.exited.wait(), timeout=5)
|
||||
await process.wait()
|
||||
done, _ = await asyncio.wait({stopping}, timeout=0.05)
|
||||
assert not done, "kill returned before stdout/stderr reached EOF"
|
||||
assert session.finished_at is None
|
||||
|
||||
process.stdout.feed_data(b"tail\n")
|
||||
process.stderr.feed_data(b"error tail\n")
|
||||
process.stdout.feed_eof()
|
||||
process.stderr.feed_eof()
|
||||
await asyncio.wait_for(stopping, timeout=5)
|
||||
|
||||
assert session._stdout_task.done()
|
||||
assert session._stderr_task.done()
|
||||
poll = await session.poll(yield_time_ms=0, max_output_chars=1000)
|
||||
assert poll.done is True
|
||||
assert poll.output == "head\ntail\n\nSTDERR:\nerror tail\n"
|
||||
assert not poll.output_incomplete
|
||||
finally:
|
||||
release.set()
|
||||
await asyncio.gather(session._stdout_task, session._stderr_task)
|
||||
process.stdout.feed_eof()
|
||||
process.stderr.feed_eof()
|
||||
await asyncio.gather(stopping, return_exceptions=True)
|
||||
await _cleanup_session(session)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_exec_session_poll_reaps_after_exit():
|
||||
process = _mock_session_process(pid=2002, returncode=0)
|
||||
process = _MockProcess(pid=2004, returncode=0, stdout=b"hi\n")
|
||||
session = _ExecSession(
|
||||
session_id="def",
|
||||
process=process,
|
||||
@@ -387,12 +536,16 @@ async def test_exec_session_poll_reaps_after_exit():
|
||||
)
|
||||
try:
|
||||
with patch("nanobot.agent.tools.shell._reap_pid") as reap:
|
||||
await asyncio.wait_for(session.wait_exit(), timeout=5)
|
||||
poll = await session.poll(yield_time_ms=0, max_output_chars=1000)
|
||||
assert poll.done is True
|
||||
assert poll.exit_code == 0
|
||||
reap.assert_called_once_with(2002)
|
||||
assert poll.output == "hi\n"
|
||||
assert poll.finish_reason == "exit"
|
||||
process.kill.assert_not_called()
|
||||
reap.assert_called_with(2004)
|
||||
finally:
|
||||
await asyncio.gather(session._stdout_task, session._stderr_task, return_exceptions=True)
|
||||
await _cleanup_session(session)
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform == "win32", reason="zombie reaping is a Unix waitpid concern")
|
||||
|
||||
Reference in new issue
Block a user