-
Notifications
You must be signed in to change notification settings - Fork 118
fix(sdk): raise asyncio StreamReader buffer in Python AsyncHostTransport #2760
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
base: main
Are you sure you want to change the base?
Changes from all commits
56a3e06
54e24af
ba392e6
4ffd1e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -493,3 +493,85 @@ async def test_reuse_after_dispose(self): | |||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.dispose() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _cleanup_wrapper(cli2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TestAsyncLargeResponse: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both tests build |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Responses larger than the StreamReader buffer must not crash the reader.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_response_above_asyncio_default_streamreader_limit(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| big_payload = 'x' * (200 * 1024) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli = _mock_cli_bin({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'handshake': 'ok', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'responses': [{'data': {'content': big_payload}}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| transport = AsyncHostTransport(cli, startup_timeout_ms=5_000) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.connect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = await transport.invoke(_TEST_OP, {'query': 'big'}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert result == {'content': big_payload} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert transport.state == 'CONNECTED' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.dispose() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _cleanup_wrapper(cli) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_response_above_custom_buffer_limit_raises_protocol_error(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Setting stdout_buffer_limit_bytes below the response size should | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # surface HOST_PROTOCOL_ERROR (actionable) rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # HOST_DISCONNECTED (misleading — the host is still alive), and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # error should carry a hint to raise the buffer limit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superdoc.errors import HOST_PROTOCOL_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| big_payload = 'x' * (200 * 1024) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli = _mock_cli_bin({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'handshake': 'ok', | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'responses': [{'data': {'content': big_payload}}], | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| transport = AsyncHostTransport( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| startup_timeout_ms=5_000, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout_buffer_limit_bytes=64 * 1024, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.connect() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| process = transport._process | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert process is not None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| with pytest.raises(SuperDocError) as exc_info: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.invoke(_TEST_OP, {'query': 'big'}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert exc_info.value.code == HOST_PROTOCOL_ERROR | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert 'stdout_buffer_limit_bytes' in str(exc_info.value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # The host process must be torn down — not just the transport | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # state flipped to DISCONNECTED. Otherwise dispose() short-circuits | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # and leaves an orphaned host running. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if transport._cleanup_task is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport._cleanup_task | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert transport._process is None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert transport.state == 'DISCONNECTED' | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert process.returncode is not None | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| # dispose() after an overflow must be a safe no-op. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| await transport.dispose() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _cleanup_wrapper(cli) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_client_threads_stdout_buffer_limit_to_transport(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # End-to-end wiring check: the public AsyncSuperDocClient constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # must thread stdout_buffer_limit_bytes through SuperDocAsyncRuntime | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # into AsyncHostTransport. Without this, a silent drop in client.py | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # or runtime.py would leave the existing overflow test passing while | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| # the public API reverts to the asyncio 64 KiB default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from superdoc.client import AsyncSuperDocClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| cli = _mock_cli_bin({'handshake': 'ok'}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| client = AsyncSuperDocClient( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| env={'SUPERDOC_CLI_BIN': cli}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| stdout_buffer_limit_bytes=64 * 1024, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| transport = client._runtime._transport | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert transport._stdout_buffer_limit_bytes == 64 * 1024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _cleanup_wrapper(cli) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+531
to
+557
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this exposes the bug down at
transport.py:619-631— see the summary. the fix belongs in that branch, not here.