Skip to content

fix(tool): prevent deadlock in execute_python_code with large output#1465

Open
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
octo-patch:fix/issue-1255-execute-python-code-deadlock
Open

fix(tool): prevent deadlock in execute_python_code with large output#1465
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
octo-patch:fix/issue-1255-execute-python-code-deadlock

Conversation

@octo-patch
Copy link
Copy Markdown
Contributor

Fixes #1255

Problem

execute_python_code suffers from the same pipe-buffer deadlock as execute_shell_command (issue #1255). When the Python script writes output larger than the OS pipe buffer (typically 64 KB on Linux), the child process blocks waiting for the parent to read from the pipe, while the parent blocks in proc.wait() waiting for the child to exit — resulting in a deadlock that persists until the timeout fires.

The root cause is calling proc.wait() before draining the pipes:

await asyncio.wait_for(proc.wait(), timeout=timeout)  # deadlocks if output > 64KB
stdout, stderr = await proc.communicate()

Solution

Apply the same fix used for execute_shell_command (PR #1369): create a communicate() task upfront so stdout/stderr are drained concurrently with process completion, then shield it inside wait_for to honour the timeout without cancelling the underlying read.

communicate_task = asyncio.create_task(proc.communicate())

try:
    stdout, stderr = await asyncio.wait_for(
        asyncio.shield(communicate_task),
        timeout=timeout,
    )
    ...
except asyncio.TimeoutError:
    proc.terminate()
    stdout, stderr = await communicate_task
    ...

Testing

Added a regression test that executes a Python script writing 128 KB to stdout, verifying the full payload is received without deadlock.

When the Python script produces output larger than the OS pipe buffer
(typically 64KB), `proc.wait()` deadlocks because the child process
blocks trying to write while the parent never reads.

Apply the same fix already used for execute_shell_command: create a
`communicate()` task upfront so stdout/stderr are drained concurrently
with process completion, then shield it inside `wait_for` to honour the
timeout without cancelling the underlying read.

Also adds a regression test that writes 128 KB of output to confirm no
deadlock occurs under the default timeout.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a subprocess pipe-buffer deadlock in execute_python_code by ensuring stdout/stderr are drained concurrently with process execution, and adds a regression test for large stdout output.

Changes:

  • Start proc.communicate() immediately (via a background task) and await it with wait_for(asyncio.shield(...)) to avoid pipe-buffer deadlocks.
  • On timeout, terminate the subprocess and await the same communicate task to preserve partial output.
  • Add a regression test that runs Python code emitting 128 KiB to stdout.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/agentscope/tool/_coding/_python.py Reworks subprocess waiting to avoid deadlock on large stdout/stderr output.
tests/tool_test.py Adds a regression test covering large stdout output without deadlock.

Comment on lines +55 to +59
communicate_task = asyncio.create_task(proc.communicate())

try:
await asyncio.wait_for(proc.wait(), timeout=timeout)
stdout, stderr = await proc.communicate()
stdout, stderr = await asyncio.wait_for(
asyncio.shield(communicate_task),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@octo-patch The suggestion looks reasonable, please consider applying it

Comment on lines 71 to 75
returncode = -1
try:
proc.terminate()
stdout, stderr = await proc.communicate()
stdout, stderr = await communicate_task
stdout_str = stdout.decode("utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:execute_shell_command deadlocks when subprocess output exceeds pipe buffer

3 participants