diff --git a/starlette/testclient.py b/starlette/testclient.py index 978f8f506..1037e38cb 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -12,6 +12,7 @@ from urllib.parse import unquote, urljoin import anyio +import anyio.from_thread import httpx from anyio.streams.stapled import StapledObjectStream @@ -398,7 +399,9 @@ def _portal_factory(self) -> typing.Generator[anyio.abc.BlockingPortal, None, No if self.portal is not None: yield self.portal else: - with anyio.start_blocking_portal(**self.async_backend) as portal: + with anyio.from_thread.start_blocking_portal( + **self.async_backend + ) as portal: yield portal def _choose_redirect_arg( @@ -714,7 +717,7 @@ def websocket_connect( def __enter__(self) -> "TestClient": with contextlib.ExitStack() as stack: self.portal = portal = stack.enter_context( - anyio.start_blocking_portal(**self.async_backend) + anyio.from_thread.start_blocking_portal(**self.async_backend) ) @stack.callback