From 5b50d594d4939d78bbf65ef5ce71bfcdc4bff7e7 Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Wed, 18 Dec 2024 14:29:11 +0100 Subject: [PATCH] Safely copy crt clients The request pipeline currently deepcopies the config since it can be mutated by plugins. This was causing issues when copying a CRT client, which has un-copiable components to it. This implements the deepcopy method to behave better. It may be necessary in the future to either remove the deepcopy, or modify this implementation to violate the intent of a deepcopy somewhat. I'm a bit concerned about not sharing the connection pool. --- python-packages/smithy-http/smithy_http/aio/crt.py | 11 ++++++++--- .../smithy-http/tests/unit/aio/test_crt.py | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 python-packages/smithy-http/tests/unit/aio/test_crt.py diff --git a/python-packages/smithy-http/smithy_http/aio/crt.py b/python-packages/smithy-http/smithy_http/aio/crt.py index b064bab5c..6cb635a44 100644 --- a/python-packages/smithy-http/smithy_http/aio/crt.py +++ b/python-packages/smithy-http/smithy_http/aio/crt.py @@ -5,6 +5,7 @@ import asyncio from collections.abc import AsyncGenerator, AsyncIterable, Awaitable from concurrent.futures import Future +from copy import deepcopy from io import BytesIO from threading import Lock from typing import TYPE_CHECKING, Any @@ -178,9 +179,7 @@ def __init__( client. """ _assert_crt() - self._config = ( - AWSCRTHTTPClientConfig() if client_config is None else client_config - ) + self._config = client_config or AWSCRTHTTPClientConfig() if eventloop is None: eventloop = _AWSCRTEventLoop() self._eventloop = eventloop @@ -334,3 +333,9 @@ async def _consume_body_async( dest.write(chunk) # Should we call close here? Or will that make the crt unable to read the last # chunk? + + def __deepcopy__(self, memo: Any) -> "AWSCRTHTTPClient": + return AWSCRTHTTPClient( + eventloop=self._eventloop, + client_config=deepcopy(self._config), + ) diff --git a/python-packages/smithy-http/tests/unit/aio/test_crt.py b/python-packages/smithy-http/tests/unit/aio/test_crt.py new file mode 100644 index 000000000..1afabc3d8 --- /dev/null +++ b/python-packages/smithy-http/tests/unit/aio/test_crt.py @@ -0,0 +1,8 @@ +from copy import deepcopy + +from smithy_http.aio.crt import AWSCRTHTTPClient + + +def test_deepcopy_client() -> None: + client = AWSCRTHTTPClient() + deepcopy(client)