Skip to content

Commit 12ff66d

Browse files
authored
[3.12] Fix AsyncResolver not using the loop argument (#10951)
fixes #10787
1 parent b00739b commit 12ff66d

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGES/10951.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :py:class:`~aiohttp.resolver.AsyncResolver` not using the ``loop`` argument in versions 3.x where it should still be supported -- by :user:`bdraco`.

aiohttp/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def __init__(
9494
if aiodns is None:
9595
raise RuntimeError("Resolver requires aiodns library")
9696

97-
self._loop = asyncio.get_running_loop()
97+
self._loop = loop or asyncio.get_running_loop()
9898
self._manager: Optional[_DNSResolverManager] = None
9999
# If custom args are provided, create a dedicated resolver instance
100100
# This means each AsyncResolver with custom args gets its own

tests/test_resolver.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,37 @@ async def test_async_resolver_close_with_none_resolver() -> None:
711711

712712
# This should not raise AttributeError
713713
await resolver.close()
714+
715+
716+
@pytest.mark.skipif(aiodns is None, reason="aiodns required")
717+
def test_async_resolver_uses_provided_loop() -> None:
718+
"""Test that AsyncResolver uses the loop parameter when provided."""
719+
# Create a custom event loop
720+
custom_loop = asyncio.new_event_loop()
721+
722+
try:
723+
# Need to set the loop as current for get_running_loop() to work
724+
asyncio.set_event_loop(custom_loop)
725+
726+
# Create resolver with explicit loop parameter
727+
resolver = AsyncResolver(loop=custom_loop)
728+
729+
# Check that the resolver uses the provided loop
730+
assert resolver._loop is custom_loop
731+
finally:
732+
asyncio.set_event_loop(None)
733+
custom_loop.close()
734+
735+
736+
@pytest.mark.skipif(aiodns is None, reason="aiodns required")
737+
@pytest.mark.usefixtures("check_no_lingering_resolvers")
738+
async def test_async_resolver_uses_running_loop_when_none_provided() -> None:
739+
"""Test that AsyncResolver uses get_running_loop() when no loop is provided."""
740+
# Create resolver without loop parameter
741+
resolver = AsyncResolver()
742+
743+
# Check that the resolver uses the current running loop
744+
assert resolver._loop is asyncio.get_running_loop()
745+
746+
# Clean up
747+
await resolver.close()

0 commit comments

Comments
 (0)