Skip to content

Commit 805239f

Browse files
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.
1 parent 626fed6 commit 805239f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

python-packages/smithy-http/smithy_http/aio/crt.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import asyncio
66
from collections.abc import AsyncGenerator, AsyncIterable, Awaitable
77
from concurrent.futures import Future
8+
from copy import deepcopy
89
from io import BytesIO
910
from threading import Lock
1011
from typing import TYPE_CHECKING, Any
@@ -178,9 +179,7 @@ def __init__(
178179
client.
179180
"""
180181
_assert_crt()
181-
self._config = (
182-
AWSCRTHTTPClientConfig() if client_config is None else client_config
183-
)
182+
self._config = client_config or AWSCRTHTTPClientConfig()
184183
if eventloop is None:
185184
eventloop = _AWSCRTEventLoop()
186185
self._eventloop = eventloop
@@ -334,3 +333,9 @@ async def _consume_body_async(
334333
dest.write(chunk)
335334
# Should we call close here? Or will that make the crt unable to read the last
336335
# chunk?
336+
337+
def __deepcopy__(self, memo: Any) -> "AWSCRTHTTPClient":
338+
return AWSCRTHTTPClient(
339+
eventloop=self._eventloop,
340+
client_config=deepcopy(self._config),
341+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from copy import deepcopy
2+
3+
from smithy_http.aio.crt import AWSCRTHTTPClient
4+
5+
6+
def test_deepcopy_client() -> None:
7+
client = AWSCRTHTTPClient()
8+
deepcopy(client)

0 commit comments

Comments
 (0)