You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ServiceBrowsers have two sources of truth that may not agree #720
Even the cache has a source of truth problem since there can be two or more records in the cache that are eq even though they have different TTLs. The __eq__ with different TTLs is by design, but adding an entry to the cache MUST always replace an existing entry since a record can't be both expired and non-expired (or stable/recent) for that matter.
We should document which part of the dnscache is thread safe and which part is not
Threadsafe
Get
Get by details
Get by name
Get by server
Not thread safe
Add
Remove
We could also provide non threadsafe versions of gets since 99% of calls are called from the event loop and don't need to be threadsafe and all the list copies are expensive.
We can also do o(1) lookups (assumes no collisions) for specific record types if we don't need to match any record with name. We should change the typing to be a union of all the dns classes and do an isinstance check before doing that. Should be much more efficient once up and running for a few seconds since the known answer (cache) can get quite long quickly
ServiceInfo and new service-browsers/listeners are the primary source of the non threadsafe lookups
The original reaper thread was the source of non threadsafe lookups and removes. Thinking back, these were always unsafe removes and we could have removed whole branches of newly added records before the extra thread as eliminated. We should remove the key error guard in remove and document it's not threadsafe since it really isn't and the exception guard is hiding any risky removals
We should make all calls to RecordUpdateListener threadsafe from the event loop and document that update_records and update_records_complete must be called in the event loop since the cache function gets
Calling RecordManager.add_listener with a set of questions is making non threadsafe calls. We should arrange for that to be run in the event loop instead since running it immediately isn't needed. This will guarantee all calls to update records and update records complete are running in the event loop.
Maybe these should be protected calls since it will be very obvious when someone is implementing something unsafe and it's unlikely someone using zeroconf will know what is threadsafe and what is not or even know about threading issues to being with
Since update_records and update_records_complete haven't shipped publicly yet we can make them protected and save everyone the headache later when someone discovers they aren't threadsafe. Their original now deprecated counterpart update_record isn't threadsafe either and never has been. Its obviously been a source of problems since all the threadsafety guards that had to be added to the cache class.
Eliminating the separate reaper thread probably solved most of the common race issues
All the get functions should probably have been named get_threadsafe. We really can't change then now since callers rely on them
At least we make a base class maybe AsyncDnsCache and put all the functions that aren't threadsafe in there, renaming them async_add etc and then keep the existing DnsCache class name and put the threadsafe versions in there. It should make it more obvious about what is going on and reduce the chance of making a mistake.
Making update_records and update_records_complete protected will probably be confusing to implementors using zeroconf. Maybe it makes more sense to prefix them with async_ instead so it's clear they need to be run in the loop (previously the select loop)
I think this makes the most sense and then we will see quickly if we have any unsafe calls in the code base
The cache cleanup task knows too much about the cache internals. It should instead ask the cache to expire everything that's ready to expire snd have it return a list of records to pass to async_update_records
Maybe we should move DnsCache.cache to be an internal and provide .cache as a stable property for externally callers that want a complete cache dump like netdisco so it doesn't break every time we change the internals which has had to happen a lot in the last year
We should fix that and always call back the updates since the TTL the service browser knows about can be wrong since its storing a copy of the DNSPointer before its updated
Activity
bdraco commentedon Jun 18, 2021
Ideally we eliminate
self._servicesand just use the cache since this would solve the disagreements about truth.The cache was likely not used before because it didn't perform well. It likely can still be improved from a performance standpoint.
bdraco commentedon Jun 18, 2021
Even the cache has a source of truth problem since there can be two or more records in the cache that are eq even though they have different TTLs. The
__eq__with different TTLs is by design, but adding an entry to the cache MUST always replace an existing entry since a record can't be both expired and non-expired (or stable/recent) for that matter.bdraco commentedon Jun 18, 2021
The cache should probably be implemented as a Dict[DNSRecord, DNSRecord] (https://stackoverflow.com/questions/22386287/python-get-item-from-set-based-on-key).
Need to check to see if that would perform better
bdraco commentedon Jun 18, 2021
We should document which part of the dnscache is thread safe and which part is not
Threadsafe
Get
Get by details
Get by name
Get by server
Not thread safe
Add
Remove
We could also provide non threadsafe versions of gets since 99% of calls are called from the event loop and don't need to be threadsafe and all the list copies are expensive.
bdraco commentedon Jun 18, 2021
Maybe a get and get_threadsafe
bdraco commentedon Jun 18, 2021
We can also do o(1) lookups (assumes no collisions) for specific record types if we don't need to match any record with name. We should change the typing to be a union of all the dns classes and do an isinstance check before doing that. Should be much more efficient once up and running for a few seconds since the known answer (cache) can get quite long quickly
bdraco commentedon Jun 18, 2021
ServiceInfo and new service-browsers/listeners are the primary source of the non threadsafe lookups
The original reaper thread was the source of non threadsafe lookups and removes. Thinking back, these were always unsafe removes and we could have removed whole branches of newly added records before the extra thread as eliminated. We should remove the key error guard in remove and document it's not threadsafe since it really isn't and the exception guard is hiding any risky removals
bdraco commentedon Jun 18, 2021
We should make all calls to RecordUpdateListener threadsafe from the event loop and document that update_records and update_records_complete must be called in the event loop since the cache function gets
bdraco commentedon Jun 18, 2021
Calling RecordManager.add_listener with a set of questions is making non threadsafe calls. We should arrange for that to be run in the event loop instead since running it immediately isn't needed. This will guarantee all calls to update records and update records complete are running in the event loop.
Maybe these should be protected calls since it will be very obvious when someone is implementing something unsafe and it's unlikely someone using zeroconf will know what is threadsafe and what is not or even know about threading issues to being with
bdraco commentedon Jun 18, 2021
Since update_records and update_records_complete haven't shipped publicly yet we can make them protected and save everyone the headache later when someone discovers they aren't threadsafe. Their original now deprecated counterpart update_record isn't threadsafe either and never has been. Its obviously been a source of problems since all the threadsafety guards that had to be added to the cache class.
Eliminating the separate reaper thread probably solved most of the common race issues
bdraco commentedon Jun 18, 2021
All the get functions should probably have been named get_threadsafe. We really can't change then now since callers rely on them
At least we make a base class maybe AsyncDnsCache and put all the functions that aren't threadsafe in there, renaming them async_add etc and then keep the existing DnsCache class name and put the threadsafe versions in there. It should make it more obvious about what is going on and reduce the chance of making a mistake.
bdraco commentedon Jun 18, 2021
We should document all the handlers are not threadsafe as well (and they never have been). Maybe rename the functions to prefix with async_
bdraco commentedon Jun 18, 2021
Making update_records and update_records_complete protected will probably be confusing to implementors using zeroconf. Maybe it makes more sense to prefix them with async_ instead so it's clear they need to be run in the loop (previously the select loop)
I think this makes the most sense and then we will see quickly if we have any unsafe calls in the code base
bdraco commentedon Jun 18, 2021
The cache cleanup task knows too much about the cache internals. It should instead ask the cache to expire everything that's ready to expire snd have it return a list of records to pass to async_update_records
bdraco commentedon Jun 18, 2021
Maybe we should move DnsCache.cache to be an internal and provide .cache as a stable property for externally callers that want a complete cache dump like netdisco so it doesn't break every time we change the internals which has had to happen a lot in the last year
bdraco commentedon Jun 19, 2021
The two sources of truth definitely get out of sync since https://github.com/jstasiak/python-zeroconf/blob/master/zeroconf/_handlers.py#L325
We should fix that and always call back the updates since the TTL the service browser knows about can be wrong since its storing a copy of the DNSPointer before its updated