Skip to content
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

Close request when initial response fails and propagate error #454

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ def _classify_error(
request_future, response_future,
)
except Exception as e:
if request_future is not None and not request_future.done:
if request_future is not None and not request_future.done():
request_future.set_exception($4T(e))
if response_future is not None and not response_future.done:
if response_future is not None and not response_future.done():
response_future.set_exception($4T(e))

# Make sure every exception that we throw is an instance of $4T so
Expand Down
32 changes: 25 additions & 7 deletions packages/smithy-http/src/smithy_http/aio/crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,15 @@ def on_response(
kind=FieldPosition.HEADER,
)

self._response_future.set_result(
AWSCRTHTTPResponse(
status=status_code,
fields=fields,
body=self._body,
)
response = AWSCRTHTTPResponse(
status=status_code,
fields=fields,
body=self._body,
)
if status_code != 200 and status_code >= 300:
self._response_future.set_exception(CRTErrorResponse(response))
else:
self._response_future.set_result(response)

async def await_response(self) -> AWSCRTHTTPResponse:
return await asyncio.wrap_future(self._response_future)
Expand All @@ -193,6 +195,18 @@ def _cancel(self, completion_future: ConcurrentFuture[int | Exception]) -> None:
self._response_future.cancel()


class CRTErrorResponse(Exception):
def __init__(self, response: AWSCRTHTTPResponse) -> None:
self._status = response.status
self._response = response

super().__init__(f"Request failed with status code {self._status}")

@property
def response(self) -> AWSCRTHTTPResponse:
return self._response
Copy link
Contributor

Choose a reason for hiding this comment

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

What actually gets surfaced to the end user with the stack trace here? Is it only the message being passed to the base Exception constructor? Ideally we have more context if we can.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This exception gets caught in the AWSCRTHTTPClient.send method, which then closes the request body and returns the response upwards. The client code then deserializes the response using its deserializers and will create the appropriate service exception. An example for an expired security token:

Traceback (most recent call last):
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/client.py", line 595, in _execute_operation
    return await self._handle_execution(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/client.py", line 751, in _handle_execution
    return await self._finalize_execution(interceptor_chain, output_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/client.py", line 1003, in _finalize_execution
    raise context.response
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/client.py", line 717, in _handle_execution
    raise output_context.response
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/client.py", line 916, in _handle_attempt
    output = await deserialize(response_context.transport_response, config)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alexwoo/python/test-lexruntime/lexruntime/deserialize.py", line 403, in _deserialize_start_conversation
    raise e
lexruntime.models.UnknownApiError: ExpiredTokenException: The security token included in the request is expired



ConnectionPoolKey = tuple[str, str, int | None]
ConnectionPoolDict = dict[ConnectionPoolKey, "crt_http.HttpClientConnection"]

Expand Down Expand Up @@ -251,7 +265,11 @@ async def send(
crt_stream.completion_future.add_done_callback(
partial(self._close_input_body, body=crt_body)
)
return await response_factory.await_response()
try:
return await response_factory.await_response()
except CRTErrorResponse as e:
await close(crt_body)
return e.response

def _close_input_body(
self, future: ConcurrentFuture[int], *, body: "BufferableByteStream | BytesIO"
Expand Down