Skip to content

ServiceEvent::ServiceRemoved doesn't work #54

Closed

Description

@WilliamVenner

Hi,

I noticed that ServiceEvent::ServiceRemoved never fires. I tried shutting down the service daemon, I tried stop_browse on the service daemon, but the other computer never sees that my computer stopped listening/browsing/broadcasting.

Both computers are Windows.

Am I understanding correctly this event should fire when a device goes away?

Activity

  1. keepsimple1 commented on Aug 10, 2022

    @keepsimple1
    Owner

    Can you please try to call unregister ? If it didn't work, please let me know.

  2. WilliamVenner commented on Aug 10, 2022

    @WilliamVenner
    Author

    Hi, that does work!

    Should the event be firing when a peer exits ungracefully though?

    I want to show to the user on the UI that a peer has gone away, even if ungracefully.

  3. WilliamVenner commented on Aug 10, 2022

    @WilliamVenner
    Author

    While we're here... is there any way to uniquely identify a service/peer? What's stopping me advertising as a fake device or something? Am I misunderstanding the mDNS protocol, or surely is there a way to get the responder's IP address (and not the one they specify in the advertisement, but the actual IP address from the socket)?

  4. keepsimple1 commented on Aug 11, 2022

    @keepsimple1
    Owner

    I don't think our current implementation detects peer going away ungracefully, even though we use defined TTL values. I can look into this a bit.

    I'm not sure about the identity of a service/peer other than it's endpoint (instance name, IP:port), but for mDNS, there is spec for unique resource records identified by its name, rrtype, and rrclass. A resource record can be unique or shared. See this RFC section. Currently we haven't implemented the Conflict Resolution mentioned in the RFC yet.

    Regarding the IP address, we distinguish between the responder's IP and the announced service IP. The mDNS protocol specifies the service IP via A (for IPv4) records. Currently we don't log / keep the responder's IP, one reason being that socket2 0.4 branch does not have a safe recv_from API. We could make an effort to get the responder IP, but in the general mDNS sense, it is not useful. At least, I'm not aware of use cases.

  5. WilliamVenner commented on Aug 11, 2022

    @WilliamVenner
    Author

    I don't think our current implementation detects peer going away ungracefully, even though we use defined TTL values. I can look into this a bit.

    For my application, I want a list of discovered devices, which removes devices as they go away, just like a Wi-Fi list or something. It would definitely be nice UX. The other issue with not being able to detect when a device goes away is that, if a router gives the IP address to a different device, and then it tries to broadcast itself with mdns-sd, it doesn't seem to... update that state? It won't actually post a ServiceFound event because it thinks the device has already spoken to it, when in fact it's a different device, just with the same IP address. (Honestly that might be a bug in my code, but it would be worth observing)

    Regarding the IP address, we distinguish between the responder's IP and the announced service IP.

    I think it's important for security to not trust the advertised IP address(es) at all, at least for my application. In a worst case scenario, my application can grant an attacker (virtually) RAT access to the machine if they pull some dirty tricks with the advertised IP address. Maybe I'm being paranoid, but it definitely would be nice to have the actual socket IP address of the advertising service.

  6. keepsimple1 commented on Sep 1, 2022

    @keepsimple1
    Owner

    For my application, I want a list of discovered devices, which removes devices as they go away, just like a Wi-Fi list or something. It would definitely be nice UX.

    How quickly would you like to detect the devices going away in this use case?

  7. WilliamVenner commented on Sep 2, 2022

    @WilliamVenner
    Author

    10 seconds would probably make sense from a UI perspective. It would be nice if that was configurable, of course.

  8. dalesmith3 commented on Apr 18, 2023

    @dalesmith3

    The RFCs do have some specs on TTL values. Let's see. https://datatracker.ietf.org/doc/html/rfc6762#section-10

    As a general rule, the recommended TTL value for Multicast DNS
    resource records with a host name as the resource record's name
    (e.g., A, AAAA, HINFO) or a host name contained within the resource
    record's rdata (e.g., SRV, reverse mapping PTR record) SHOULD be 120
    seconds.

    The recommended TTL value for other Multicast DNS resource records is
    75 minutes.

    2 minutes sure seems like a long time. (and 75 even more so!), so it's always best to send out those "going away" messages (can't remember what they are called right now).

  9. dalepsmith commented on Apr 18, 2023

    @dalepsmith
    Contributor

    Doh! Sorry about repeated info on the TTL values. Have to read more carefully.

  10. keepsimple1 commented on Apr 19, 2023

    @keepsimple1
    Owner

    No worries. Btw, the same spec also talks about the difficulty / trade-off of detecting stale service / rdata due to the long TTL:

    If Multicast DNS resource records follow the recommendation and have a TTL of 75
       minutes, that means that stale data could persist in the system for a
       little over an hour.  Making the default RR TTL significantly lower
       would reduce the lifetime of stale data, but would produce too much
       extra traffic on the network.  Various techniques are available to
       minimize the impact of such stale data, outlined in the five
       subsections below.
    

    In addition to the "goodbye" going away packet, the following method seems interesting to me too:

    https://datatracker.ietf.org/doc/html/rfc6762#section-10.4

    Sometimes a cache record can be determined to be stale when a client
       attempts to use the rdata it contains, and the client finds that
       rdata to be incorrect.
    <snip>
     The software implementing the Multicast DNS resource record cache
       should provide a mechanism so that clients detecting stale rdata can
       inform the cache.
    

    We can probably look into adding support for such technique.

  11. keepsimple1 commented on May 6, 2023

    @keepsimple1
    Owner

    https://datatracker.ietf.org/doc/html/rfc6762#section-10.4
    Cache Flush on Failure Indication

     The software implementing the Multicast DNS resource record cache
       should provide a mechanism so that clients detecting stale rdata can
       inform the cache.
    
       When the cache receives this hint that it should reconfirm some
       record, it MUST issue two or more queries for the resource record in
       dispute.  If no response is received within ten seconds, then, even
       though its TTL may indicate that it is not yet due to expire, that
       record SHOULD be promptly flushed from the cache.
    

    Maybe we can support a new method in ServiceDaemon that allows the client ask the daemon to verify / check a given SRV data, say daemon.verify(full_service_instance_name) .

    Such an approach lets the user (i.e. the application) to decide the timing of the checks, which avoids a hard-coded / rigid time setting inside this library. And such API does not add any overhead for other applications that don't need to such periodical checks.

  12. oscartbeaumont commented on Nov 6, 2024

    @oscartbeaumont

    I'm also running into this issue.

    We are building an application that needs to talk with a piece of AV hardware that advertises it's HTTP API at _http._application._tcp.local.. Our application needs to show a list of all devices and let the user choose which one to connect to.

    We are observing it taking up to 2 minutes before our application receives the ServiceEvent::ServiceRemoved event which is really bad for a user experience.

    The timeout is possibly related to the AV devices software not gracefully sending out the shutdown event as using the register.rs example with the --unregister flag always works with zero delay. We don't have any control of the software on this device so we need to somehow work around this on the listener. I also think the handing of non-graceful shutdown is really important because we can never guarantee the graceful shutdown will run (Eg. the user pulls the power cord, network drops, etc).

    In persuit of a workaround I noticed if I recreate the ServiceDaemon every 10 seconds that we can observe changes much faster but recreating the ServiceDaemon along with it's udp sockets and state seems very inefficient.

    I then noticed if I modify mdns-sd to reset the global cache when .browse is called (patch here) that I can get away with just recreating the channel by calling .browse instead of recreating the whole ServiceDaemon. Although this works I would expect it to cause issues if you were listening to multiple services in the same application as the cache is global to all of them so i'm not sure I want to actually use it.

    I think the ultimate solution for my usecase would be to allow the user to override the record's TTL (Eg. daemon.force_host_ttl(10_000)) and then event loop would take care of expiring the records when the TTL is hit and asking the network for a new value. This would obviously come with the tradeoff of more network IO but for my usecase we will only need to listen while the pairing UI is open so this is a tradeoff we can easily make.

    daemon.verify would also work for my usercase and would give the user much finer grain control than .force_host_ttl so would also be happy if that ends up being the solution here.

  13. keepsimple1 commented on Nov 6, 2024

    @keepsimple1
    Owner

    daemon.verify would also work for my usercase and would give the user much finer grain control than .force_host_ttl so would also be happy if that ends up being the solution here.

    Agreed, I will work on implement daemon.verify() and hopefully post a PR by this weekend. (I felt sorry about delaying this feature so far, thanks for the details about your use case).

  14. keepsimple1 commented on Nov 9, 2024

    @keepsimple1
    Owner

    @oscartbeaumont I've posted a PR #267 to fix this issue. Please give it a try and let me know what you think when you have time. Cheers!

    UPDATE: I've updated (simplified) the API in the PR based on my understanding of the common use case. Please let me know your thoughts which version works better.

  15. added
    enhancement
    on Nov 9, 2024
  16. keepsimple1 commented on Nov 14, 2024

    @keepsimple1
    Owner

    @oscartbeaumont Just a quick check, did you have chance to look at the verify API? Would it work for you?

  17. keepsimple1 commented on Nov 17, 2024

    @keepsimple1
    Owner

    PR #267 is merged. @oscartbeaumont (or anyone) if you could give it a try and let me know your feedback, it will be awesome! Thanks!

  18. oscartbeaumont commented on Nov 18, 2024

    @oscartbeaumont

    Super sorry about not responding earlier. I will give it a shot sometime this week and report back. Thank you!

  19. oscartbeaumont commented on Nov 20, 2024

    @oscartbeaumont

    Sorry again for the delay in testing but the changes are working perfectly! Thank you for getting a fix so quickly!

  20. keepsimple1 commented on Nov 20, 2024

    @keepsimple1
    Owner

    no problem, thank you for testing! really appreciated! So happy to be able to resolve this issue now ;-).

    As always, if anyone see this issue again, feel free to re-open it or create a new issue. Cheers!

  21. hanaTsuk1 commented on Dec 14, 2025

    @hanaTsuk1

    @keepsimple1
    I suggest adding this feature to the examples. It took me a while to find it, and I think it's important for other users as well. Thank you.

  22. keepsimple1 commented on Dec 14, 2025

    @keepsimple1
    Owner

    @hanaTsuk1 I can do that. By "this feature", are you referring to the unregister() part, or the verify() part? , or both?

  23. hanaTsuk1 commented on Dec 15, 2025

    @hanaTsuk1

    @hanaTsuk1 I can do that. By "this feature", are you referring to the unregister() part, or the verify() part? , or both?

    Both, please

  24. keepsimple1 commented on Dec 18, 2025

    @keepsimple1
    Owner

    Will do it before the new year ;-)

  25. keepsimple1 commented on Dec 31, 2025

    @keepsimple1
    Owner

    Added --verify for the query example . (PR #412 merged) . --unregister already exists for the register example.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions