Repository navigation
Python: Extend max output limit to stderr #9795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
72 changes: 72 additions & 0 deletions
72
python/packages/tools/agent_framework_tools/shell/_capture.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Bounded concurrent subprocess output capture.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from typing import cast | ||
|
|
||
| from ._truncate import _OutputBuffer # pyright: ignore[reportPrivateUsage] - shared shell capture | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| READ_CHUNK = 64 * 1024 | ||
| _DRAIN_GRACE = 2.0 | ||
|
|
||
|
|
||
| class _ProcessOutput: # pyright: ignore[reportUnusedClass] - used by shell executors | ||
| """Own pipe readers independently of a command's execution timeout.""" | ||
|
|
||
| def __init__(self, proc: asyncio.subprocess.Process, *, max_stdout_bytes: int, max_stderr_bytes: int) -> None: | ||
| self._proc = proc | ||
| self.stdout = _OutputBuffer(max_stdout_bytes) | ||
| self.stderr = _OutputBuffer(max_stderr_bytes) | ||
| self._readers = ( | ||
| asyncio.create_task(self._read(proc.stdout, self.stdout)), | ||
| asyncio.create_task(self._read(proc.stderr, self.stderr)), | ||
| ) | ||
| self._waiter = asyncio.create_task(proc.wait()) | ||
| self._completion = asyncio.create_task(self._wait()) | ||
|
|
||
| @staticmethod | ||
| async def _read(stream: asyncio.StreamReader | None, output: _OutputBuffer) -> None: | ||
| if stream is None: | ||
| return | ||
| while chunk := await stream.read(READ_CHUNK): | ||
| output.append(chunk) | ||
|
|
||
| async def _wait(self) -> None: | ||
| await asyncio.gather(*self._readers, self._waiter) | ||
|
|
||
| async def wait(self, timeout: float | None) -> None: | ||
| await asyncio.wait_for(asyncio.shield(self._completion), timeout=timeout) | ||
|
|
||
| async def finish(self) -> None: | ||
| """Drain after termination without waiting forever for inherited pipes.""" | ||
| try: | ||
| await self.wait(_DRAIN_GRACE) | ||
| except asyncio.TimeoutError: | ||
| logger.warning("subprocess output did not close after termination; stopping pipe readers") | ||
| finally: | ||
| await self.close() | ||
|
|
||
| async def close(self) -> None: | ||
| tasks = (*self._readers, self._waiter, self._completion) | ||
| for task in tasks: | ||
| if not task.done(): | ||
| task.cancel() | ||
| try: | ||
| await asyncio.gather(*tasks, return_exceptions=True) | ||
| finally: | ||
| # Process has no public read-pipe close method. Use its transport | ||
| # only to close owned output pipes, not to terminate the process. | ||
| transport = cast( | ||
| asyncio.SubprocessTransport, | ||
| self._proc._transport, # pyright: ignore[reportAttributeAccessIssue, reportUnknownMemberType] - absent from typeshed | ||
| ) | ||
| for fd in (1, 2): | ||
| pipe = transport.get_pipe_transport(fd) | ||
| if pipe is not None: | ||
| pipe.close() | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.