Skip to content

Commit

Permalink
Safely copy crt clients
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JordonPhillips committed Dec 20, 2024
1 parent 626fed6 commit 805239f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions python-packages/smithy-http/smithy_http/aio/crt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
)
8 changes: 8 additions & 0 deletions python-packages/smithy-http/tests/unit/aio/test_crt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from copy import deepcopy

from smithy_http.aio.crt import AWSCRTHTTPClient


def test_deepcopy_client() -> None:
client = AWSCRTHTTPClient()
deepcopy(client)

0 comments on commit 805239f

Please sign in to comment.