Skip to content

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Jun 13, 2025

What do these changes do?

This PR adds a close() method to the DNSResolver class to allow for explicit cleanup of resources. The implementation includes:

  • New close() method that cleanly shuts down the resolver by canceling timers, removing file descriptors, and closing the pycares channel
  • Async context manager support (__aenter__ and __aexit__) for automatic cleanup when using async with
  • Improved __del__ method that automatically calls cleanup when the resolver is garbage collected
  • Comprehensive test coverage for all new functionality

Are there changes in behavior for the user?

  • New feature: Users can now explicitly close resolvers with await resolver.close()
  • New feature: Resolvers can be used as async context managers: async with aiodns.DNSResolver() as resolver:
  • Improved cleanup: Resolvers will automatically clean up resources when garbage collected (though explicit cleanup is still recommended)
  • No breaking changes: All existing functionality remains unchanged

Related issue number

Fixes #165

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

Copy link

codecov bot commented Jun 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.76%. Comparing base (9a0bcda) to head (24f943c).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #166      +/-   ##
==========================================
+ Coverage   93.85%   96.76%   +2.91%     
==========================================
  Files           3        3              
  Lines         374      557     +183     
  Branches       29       36       +7     
==========================================
+ Hits          351      539     +188     
+ Misses         14       10       -4     
+ Partials        9        8       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco requested a review from Copilot June 13, 2025 14:29
@bdraco bdraco marked this pull request as ready for review June 13, 2025 14:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces explicit resource cleanup to DNSResolver by adding a close() method, async context manager support, and an improved __del__ for automatic cleanup. It also updates tests to cover the new functionality, bumps the minimum pycares dependency, and updates the documentation.

  • Add _cleanup(), async close(), __aenter__/__aexit__, and enhanced __del__ in DNSResolver
  • Extend test suite with close, idempotence, destructor, and context‐manager tests
  • Bump pycares requirement to >=4.9.0 and document the new API

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
tests/test_aiodns.py New tests for close(), context manager, and __del__ cleanup; updated tearDown to call close()
setup.py Updated pycares minimum version in install_requires
aiodns/init.py Implemented _cleanup(), close(), async context manager, and refined destructor behavior
README.rst Documented close() method and async context manager usage
Comments suppressed due to low confidence (2)

setup.py:24

  • Bumping the minimum pycares version to 4.9.0 may affect users upgrading from older versions. Ensure this change is documented in the changelog and migration notes.
    install_requires=['pycares>=4.9.0'],

aiodns/init.py:369

  • [nitpick] In __del__, calling _cleanup() may raise if the loop or channel state is invalid. Wrap the call in try/except to suppress exceptions during garbage collection.
    def __del__(self) -> None:

bdraco and others added 2 commits June 13, 2025 09:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@bdraco
Copy link
Member Author

bdraco commented Jun 13, 2025

Thanks. I'll see if I can do a release before lunch

@bdraco bdraco merged commit 78cd600 into master Jun 13, 2025
21 checks passed
@bdraco bdraco deleted the close branch June 13, 2025 16:14
@bdraco bdraco changed the title Add explict close method Add explicit close method Jun 13, 2025
@bdraco bdraco mentioned this pull request Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update pycares required version to 4.8.0 on setup.py is needed
2 participants