Skip to content

ServiceBrowsers have two sources of truth that may not agree #720

Closed

Description

@bdraco

self._services and self.zc.cache

Ideally we use the new function in #719 to generate the query

Activity

  1. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    Ideally we eliminate self._services and 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.

  2. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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.

  3. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  4. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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.

  5. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    Maybe a get and get_threadsafe

  6. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  7. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  8. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  9. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  10. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  11. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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.

  12. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    We should document all the handlers are not threadsafe as well (and they never have been). Maybe rename the functions to prefix with async_

  13. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  14. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  15. bdraco commented on Jun 18, 2021

    @bdraco
    MemberAuthor

    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

  16. bdraco commented on Jun 19, 2021

    @bdraco
    MemberAuthor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions