[Bug]: BashTool times out when command output exceeds stream buffer

Author: wuwenbo0626Created Sep 8, 2026Updated Sep 8, 2026

What happened?

BashTool times out when a command writes more than the asyncio stream buffer (about 128 KiB) to stdout, even though the command finishes immediately.

Minimal reproduction:

python
import asyncio

from trae_agent.tools.bash_tool import _BashSession


async def main():
    session = _BashSession()
    session._timeout = 2
    await session.start()
    try:
        await session.run("python3 -c 'print(\"x\" * 131072)'")
    finally:
        await session.stop()


asyncio.run(main())

The session polls StreamReader._buffer without consuming it. Once the buffer reaches its limit, asyncio pauses the pipe transport, the child process cannot write the remaining output or the exit sentinel, and the session times out.

Observed locally:

65536 bytes   succeeds
100000 bytes  succeeds
131072 bytes  times out
150000 bytes  times out
200000 bytes  times out

What did you expect to happen?

Commands that finish within the timeout should return their output regardless of whether stdout exceeds the stream buffer. The session should consume stdout and stderr while waiting for the exit sentinel so pipe backpressure cannot block the shell.

Traceback

ToolError: timed out: bash has not returned in 2 seconds and must be restarted

What is your system, Python, dependency version?

  • OS: macOS arm64
  • Python: 3.12.14
  • trae-agent commit: e839e55

Additional information that you believe is relevant to this bug

This can affect commands that emit large build logs, test output, or generated content. The existing Bash tool tests only exercise small outputs.