fix(tool): prevent deadlock in execute_python_code with large output#1465
Open
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
Open
fix(tool): prevent deadlock in execute_python_code with large output#1465octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
Conversation
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.
Contributor
There was a problem hiding this comment.
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 withwait_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), |
Member
There was a problem hiding this comment.
@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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1255
Problem
execute_python_codesuffers from the same pipe-buffer deadlock asexecute_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 inproc.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:Solution
Apply the same fix used for
execute_shell_command(PR #1369): create acommunicate()task upfront so stdout/stderr are drained concurrently with process completion, then shield it insidewait_forto honour the timeout without cancelling the underlying read.Testing
Added a regression test that executes a Python script writing 128 KB to stdout, verifying the full payload is received without deadlock.