[Bug]: A Bash timeout or a stop doesn't stop the command on the local backend
Prerequisites
- I have searched the existing issues and discussions, and this is not a duplicate.
- This is a bug, not a usage question. (For questions, please use Discussions instead.)
Background / Description
On the local backend, a Bash command keeps running after its timeout, and after the user stops the reply.
LocalBackend.exec_shell runs the command through a shell. On a timeout it kills only that shell, then waits for the output pipe to close, in _backend.py:823-831. A program the shell started still holds the pipe, so the call waits until that program ends. A one-second timeout on sleep 3 returns after three seconds. A command that never ends by itself, like a dev server, never returns, and the agent hangs with it.
A stop is not handled at all. When the reply is cancelled, the command keeps running in the background and finishes whatever it was doing.
This hits most setups. The Bash tool uses the local backend unless it's given another one, and the service's own examples use the local workspace. The Bubblewrap backend already does it right. It starts each command in its own session and stops the whole process group, in _bubblewrap_backend.py:199-221.
The desktop PR #2427 makes the same change to this function, but it's a large PR that is still open. I can send this part on its own with tests, if that helps.
Expected: the timeout returns on time, a stop ends the command, and nothing is left running.
Error Messages
'sleep 3', timeout 1000 ms: returned after 3.0 s
'sleep 30', timeout 1000 ms: still running after 6 s
the command kept running after the stop: TrueSteps to Reproduce
import asyncio
import os
import tempfile
import time
from agentscope.tool import Bash
async def run(command: str, timeout_ms: int) -> None:
start = time.monotonic()
async for _ in await Bash()(command=command, timeout=timeout_ms):
pass
took = time.monotonic() - start
print(f"{command!r}, timeout {timeout_ms} ms: returned after {took:.1f} s")
async def stop_while_running() -> None:
mark = os.path.join(tempfile.mkdtemp(), "mark")
task = asyncio.create_task(run(f"sleep 1; touch {mark}", 10_000))
await asyncio.sleep(0.3)
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await asyncio.sleep(2)
print("the command kept running after the stop:", os.path.exists(mark))
async def main() -> None:
await run("sleep 3", 1000)
try:
await asyncio.wait_for(run("sleep 30", 1000), timeout=6)
except asyncio.TimeoutError:
print("'sleep 30', timeout 1000 ms: still running after 6 s")
await stop_while_running()
asyncio.run(main())Run it on main at 72f3f6fa. It prints the lines under Error Messages, and a sleep 30 is still running when it exits.
Environment
- AgentScope Version: 2.0.10dev (main at 72f3f6fa), and 2.0.9 has the same code
- Python Version: 3.12.3, and 3.11.15 behaves the same
- OS: Linux (aarch64)
Source: agentscope-ai/agentscope