Title: robustness: cache mutates shared `DNSRecord` instances in place to expire TTL · Issue #1780 · python-zeroconf/python-zeroconf · GitHub
Open Graph Title: robustness: cache mutates shared `DNSRecord` instances in place to expire TTL · Issue #1780 · python-zeroconf/python-zeroconf
X Title: robustness: cache mutates shared `DNSRecord` instances in place to expire TTL · Issue #1780 · python-zeroconf/python-zeroconf
Description: Problem async_mark_unique_records_older_than_1s_to_expire walks the cache and calls _async_set_created_ttl(record, now, 1), which mutates record.created and record.ttl in place via record._set_created_ttl(...), then re-_async_adds the sa...
Open Graph Description: Problem async_mark_unique_records_older_than_1s_to_expire walks the cache and calls _async_set_created_ttl(record, now, 1), which mutates record.created and record.ttl in place via record._set_crea...
X Description: Problem async_mark_unique_records_older_than_1s_to_expire walks the cache and calls _async_set_created_ttl(record, now, 1), which mutates record.created and record.ttl in place via record._set_crea...
Opengraph URL: https://github.com/python-zeroconf/python-zeroconf/issues/1780
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"robustness: cache mutates shared `DNSRecord` instances in place to expire TTL","articleBody":"## Problem\n\n`async_mark_unique_records_older_than_1s_to_expire` walks the cache and calls `_async_set_created_ttl(record, now, 1)`, which mutates `record.created` and `record.ttl` in place via `record._set_created_ttl(...)`, then re-`_async_add`s the same object. The code itself flags this — line 345 says \"It would be better if we made a copy instead of mutating the record in place, but records currently don't have a copy method.\" Listeners (`RecordUpdate.old` references obtained from `cache.async_get_unique` elsewhere in the same dispatch tick), `ServiceInfo` instances holding references to cached records, and anything in `_handlers/multicast_outgoing_queue.AnswerGroup.answers` that keys on a `DNSAddress`/`DNSPointer` instance will silently see their TTL/created flip to the new values mid-dispatch. The RFC-mandated \"expire in 1s\" path (RFC 6762 §10.2) is therefore not isolated from in-flight consumers.\n\n## Why This Matters\n\nSubtle correctness drift in hot path: any code path that captured a `DNSRecord` reference and decided \"this record is fresh, use it\" can have the same object turn stale between two of its own lines. It's also hostile to free-threading (3.14t) — concurrent reads of `record.created` / `record.ttl` while another thread is in the middle of `_set_created_ttl` are an unsynchronized write race.\n\n## Suggested Fix\n\nAdd a `copy_with_ttl(now, ttl)` (or `_cdef`-typed `_clone_with_created_ttl`) method on `DNSRecord` (and update the `.pxd` so the new method is visible on the cython hot path), then in `_async_set_created_ttl` replace the in-place mutation with `replacement = record.copy_with_ttl(now, 1); self._async_add(replacement)`. The old record will fall out of the cache when the new one supersedes it via `async_add_records`. Update the comment to remove the \"It would be better\" footnote once done.\n\n## Details\n\n| | |\n|---|---|\n| **Severity** | 🟡 Medium |\n| **Category** | robustness |\n| **Location** | `src/zeroconf/_cache.py:325-348` |\n| **Effort** | 🛠️ Moderate effort |\n\n---\n🤖 Created by Kōan from audit session\n\u003c!-- koan-audit-id: 56e459653dc4af71 -->","author":{"url":"https://github.com/bluetoothbot","@type":"Person","name":"bluetoothbot"},"datePublished":"2026-05-26T16:28:16Z","url":"https://github.com/python-zeroconf/python-zeroconf/issues/1780"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4f564103-60c5-36b9-f729-dd8aeed0f0da |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E30C:2BEA87:F7BB:15A78:6B007D35 |
| html-safe-nonce | 7b7c13c58a1c365e1d49feaaa0dba83404d8647eeae3102e74971ce56fc05ca9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMzBDOjJCRUE4NzpGN0JCOjE1QTc4OjZCMDA3RDM1IiwidmlzaXRvcl9pZCI6IjExNzE0MzM0NTUzOTc5MjgyNDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6e74780a4f74e81224b610784404d64222b7df938a1f79663ecb227e4e26d547 |
| hovercard-subject-tag | issue:4525792883 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-zeroconf/python-zeroconf/1780/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f5a6380f27a8293080f37ee3a010bde9b595405f1bcb2ecefc8ce5eabacb9188/python-zeroconf/python-zeroconf/issues/1780 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f5a6380f27a8293080f37ee3a010bde9b595405f1bcb2ecefc8ce5eabacb9188/python-zeroconf/python-zeroconf/issues/1780 |
| og:image:alt | Problem async_mark_unique_records_older_than_1s_to_expire walks the cache and calls _async_set_created_ttl(record, now, 1), which mutates record.created and record.ttl in place via record._set_crea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bluetoothbot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 82c186f13e104004ab7bdb779f3195e59dd38bfde531aaa74b76470fc2462b83 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-zeroconf/python-zeroconf git https://github.com/python-zeroconf/python-zeroconf.git |
| octolytics-dimension-user_id | 120192235 |
| octolytics-dimension-user_login | python-zeroconf |
| octolytics-dimension-repository_id | 21548731 |
| octolytics-dimension-repository_nwo | python-zeroconf/python-zeroconf |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 21548731 |
| octolytics-dimension-repository_network_root_nwo | python-zeroconf/python-zeroconf |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d1bc2aa1939001689f5ce935982c10a6138f745c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width