Skip to content

Commit 4cb8ab8

Browse files
authored
Fix resolver garbage collection during pending queries (#211)
1 parent e088cf4 commit 4cb8ab8

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

aiodns/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@ def nameservers(self) -> Sequence[str]:
128128
def nameservers(self, value: Iterable[Union[str, bytes]]) -> None:
129129
self._channel.servers = value
130130

131-
@staticmethod
132131
def _callback(
133-
fut: asyncio.Future[_T], result: _T, errorno: Optional[int]
132+
self, fut: asyncio.Future[_T], result: _T, errorno: Optional[int]
134133
) -> None:
135134
if fut.cancelled():
136135
return

tests/test_aiodns.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,5 +823,31 @@ async def mock_close() -> None:
823823
assert close_count == 2
824824

825825

826+
@pytest.mark.asyncio
827+
async def test_temporary_resolver_not_garbage_collected() -> None:
828+
"""Test temporary resolver is not garbage collected before query completes.
829+
830+
Regression test for https://github.com/aio-libs/aiodns/issues/209
831+
832+
When calling query() on a temporary resolver (not stored in a variable),
833+
the resolver should not be garbage collected before the query completes.
834+
Previously, the callback was a @staticmethod which didn't hold a reference
835+
to self, causing the resolver to be garbage collected and the query
836+
cancelled.
837+
"""
838+
# Force garbage collection to ensure any weak references are cleared
839+
gc.collect()
840+
841+
# This pattern previously failed with DNSError(24, 'DNS query cancelled')
842+
# because the resolver was garbage collected before the query completed
843+
result = await aiodns.DNSResolver(nameservers=['8.8.8.8']).query(
844+
'google.com', 'A'
845+
)
846+
847+
# Query should succeed
848+
assert result
849+
assert len(result) > 0
850+
851+
826852
if __name__ == '__main__': # pragma: no cover
827853
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)