Skip to content

Close the DNS resolver, also when a lookup is cut short - #1592

Merged
frenck merged 1 commit into
mainfrom
frenck/close-dns-resolver
Oct 2, 2026
Merged

frenck merged 1 commit into
mainfrom
frenck/close-dns-resolver

Conversation

@frenck

@frenck frenck commented Oct 2, 2026

Copy link
Copy Markdown
Owner

Breaking change

None.

Proposed change

Found while testing the debug logging. The client created a new DNSResolver for every server lookup and never closed it. Since #1569, the request timeout also covers that lookup, so a slow lookup gets cut short. The query then stays pending inside pycares, and once the event loop shuts down, the Python process can crash with a segmentation fault:

Exception ignored from cffi callback <function _query_dnsrec_cb at 0x...>:

(exit code 139). For Home Assistant, that means a crash on shutdown or restart if a Radio Browser lookup was in flight.

The resolver is now used as an async context manager, which aiodns provides for exactly this: it cancels pending queries and releases the channel, also when the lookup is cancelled.

Measured with a RadioBrowser whose lookups are cut short by a tiny timeout, 20 runs each: 18 of 20 crashed before this change, 0 of 20 after. The new tests check that the resolver is closed after a normal lookup, and after every lookup the timeout cuts short.

Type of change

  • Dependency or tooling upgrade
  • Bugfix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Deprecation (replaces or removes a feature, with a migration path)
  • Breaking change (a fix or feature that changes existing behavior)
  • Code quality, refactor, or test-only change
  • Documentation only

Additional information

Checklist

  • I fully understand the code in this pull request and can explain every line, including any AI-assisted changes.
  • The change is covered by tests, and poetry run pytest passes locally. A pull request cannot be merged unless CI is green.
  • poetry run prek run --all-files passes (lint, format, and type checks).
  • No commented-out or dead code is left in the pull request.

A new DNSResolver was created for every lookup and never closed. When the
request timeout cut a lookup short, the query stayed pending in pycares,
and the Python process could crash with a segmentation fault once the
event loop shut down.
@frenck frenck added the bugfix Inconsistencies or issues which will cause a problem for users or implementers. label Oct 2, 2026
@frenck
frenck merged commit a081367 into main Oct 2, 2026
31 checks passed
@frenck frenck mentioned this pull request Oct 2, 2026
4 of 11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Inconsistencies or issues which will cause a problem for users or implementers.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant