Fix DNS resolution in containerized environments using ThreadedResolver#35
Merged
Fix DNS resolution in containerized environments using ThreadedResolver#35
Conversation
- Replace default aiodns resolver with explicit ThreadedResolver - Uses Python's built-in socket module instead of c-ares binding - Fixes DNS failures in Home Assistant and other containers - No additional dependencies required - Minimal changes to 3 session creation points: * NavienAuthClient.__aenter__() * NavienAuthClient._ensure_session() * refresh_access_token() function Resolves DNS errors: 'DNS server returned general failure' Makes library work reliably in all containerized environments
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes DNS resolution failures in containerized environments (Home Assistant, Docker, Kubernetes) by explicitly using aiohttp's ThreadedResolver instead of relying on the default aiodns resolver, which depends on the c-ares C binding that may not be available in containers.
Key changes:
- Replace default DNS resolution with ThreadedResolver in all aiohttp.ClientSession creations
- ThreadedResolver uses Python's built-in socket module, eliminating external dependencies
- All three session creation points in auth.py are updated for consistency
src/nwp500/auth.py
Outdated
Comment on lines
+370
to
+374
| resolver = aiohttp.ThreadedResolver() | ||
| connector = aiohttp.TCPConnector(resolver=resolver) | ||
| self._session = aiohttp.ClientSession( | ||
| connector=connector, timeout=self.timeout | ||
| ) |
There was a problem hiding this comment.
The resolver and connector creation pattern is duplicated in three locations (aenter, _ensure_session, refresh_access_token). Consider extracting this into a helper method like _create_session() to reduce code duplication and make future changes easier to maintain.
- Add _create_session() helper method to NavienAuthClient - Eliminates code duplication across 3 session creation points - Improves maintainability and makes future changes easier - All 3 locations now use single source of truth for DNS resolver config - Maintains same functionality with cleaner code
Owner
Author
Update: Code Refactoring Applied ✨Thanks for the suggestion! I've refactored the code to eliminate duplication: Changes:
Before (3 duplicate blocks):resolver = aiohttp.ThreadedResolver()
connector = aiohttp.TCPConnector(resolver=resolver)
self._session = aiohttp.ClientSession(
connector=connector, timeout=self.timeout
)After (single source of truth):def _create_session(self) -> aiohttp.ClientSession:
"""Create session with ThreadedResolver for reliable DNS."""
resolver = aiohttp.ThreadedResolver()
connector = aiohttp.TCPConnector(resolver=resolver)
return aiohttp.ClientSession(connector=connector, timeout=self.timeout)Updated commit:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
DNS resolution failures in containerized environments (Home Assistant, Docker, Kubernetes):
aiodns.error.DNSError: (3, 'DNS server returned general failure')Solution
Use ThreadedResolver explicitly for all aiohttp.ClientSession creations:
Changes
NavienAuthClient.__aenter__(): Use ThreadedResolver for session creationNavienAuthClient._ensure_session(): Use ThreadedResolver for backup sessionrefresh_access_token(): Use ThreadedResolver for temporary sessionTechnical Details
Verification
✓ Linting: All checks passed (ruff format, ruff check)
✓ Syntax: Python compilation successful
✓ Runtime: ThreadedResolver successfully created in all scenarios
✓ No new type errors introduced
Impact