Skip to content

Commit

Permalink
Detect blocking calls in coroutines using BlockBuster
Browse files Browse the repository at this point in the history
  • Loading branch information
cbornet committed Feb 6, 2025
1 parent e6ed541 commit 1556000
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 234 deletions.
9 changes: 6 additions & 3 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,14 @@ async def _request(
if req_cookies:
all_cookies.load(req_cookies)

proxy_: Optional[URL] = None
if proxy is not None:
proxy = URL(proxy)
proxy_ = URL(proxy)
elif self._trust_env:
with suppress(LookupError):
proxy, proxy_auth = get_env_proxy_for_url(url)
proxy_, proxy_auth = await asyncio.to_thread(
get_env_proxy_for_url, url
)

req = self._request_class(
method,
Expand All @@ -628,7 +631,7 @@ async def _request(
expect100=expect100,
loop=self._loop,
response_class=self._response_class,
proxy=proxy,
proxy=proxy_,
proxy_auth=proxy_auth,
timer=timer,
session=self,
Expand Down
1 change: 1 addition & 0 deletions requirements/test.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-r base.in

blockbuster
coverage
freezegun
mypy; implementation_name == "cpython"
Expand Down
4 changes: 4 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ annotated-types==0.7.0
# via pydantic
async-timeout==5.0.1 ; python_version < "3.11"
# via -r requirements/runtime-deps.in
blockbuster==1.5.14
# via -r requirements/test.in
brotli==1.1.0 ; platform_python_implementation == "CPython"
# via -r requirements/runtime-deps.in
cffi==1.17.1
Expand All @@ -33,6 +35,8 @@ exceptiongroup==1.2.2
# via pytest
execnet==2.1.1
# via pytest-xdist
forbiddenfruit==0.1.4
# via blockbuster
freezegun==1.5.1
# via -r requirements/test.in
frozenlist==1.5.0
Expand Down
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from uuid import uuid4

import pytest
from blockbuster import blockbuster_ctx

from aiohttp.client_proto import ResponseHandler
from aiohttp.http import WS_KEY
Expand All @@ -33,6 +34,25 @@
IS_LINUX = sys.platform.startswith("linux")


@pytest.fixture(autouse=True)
def blockbuster() -> Iterator[None]:
with blockbuster_ctx("aiohttp") as bb:
bb.functions["io.TextIOWrapper.read"].can_block_in(
"aiohttp/client_reqrep.py", "update_auth"
)
bb.functions["os.stat"].can_block_in("aiohttp/client_reqrep.py", "update_auth")
bb.functions["os.stat"].can_block_in(
"asyncio/unix_events.py", "create_unix_server"
)
bb.functions["os.sendfile"].can_block_in(
"asyncio/unix_events.py", "_sock_sendfile_native_impl"
)
bb.functions["os.read"].can_block_in(
"asyncio/base_events.py", "subprocess_shell"
)
yield


@pytest.fixture
def tls_certificate_authority() -> trustme.CA:
if not TRUSTME:
Expand Down
Loading

0 comments on commit 1556000

Please sign in to comment.