librespot-org / librespot

Open Source Spotify client library
MIT License
4.52k stars 545 forks source link

Undiscoverable when launched at the same time as avahi-daemon #1133

Open ettom opened 1 year ago

ettom commented 1 year ago

Describe the bug Librespot (with dns-sd backend) stays in an undiscoverable state if launched closely together with avahi-daemon.

To reproduce Steps to reproduce the behavior:

  1. systemctl stop avahi-daemon
  2. Launch librespot with:
    while :; do librespot --zeroconf-port=5354 --volume-ctrl=linear --name Snapcast --bitrate 320 --backend pipe --initial-volume 100 --verbose; sleep 0.5; done

    Librespot fails to start until avahi is started:

    [2023-03-19T21:02:13Z WARN  librespot] Could not initialise discovery: Setting up dns-sd failed: DNS-SD Error: Unknown.
    [2023-03-19T21:02:13Z ERROR librespot] Discovery is unavailable and no credentials provided. Authentication is not possible.
  3. systemctl start avahi-daemon

Librespot now starts, but the instance is not discoverable until librespot is launched again. Some kind of a race condition? Additionally, restarting avahi-daemon while librespot is running (in a discoverable state) will make it undiscoverable until restarted.

Log:

[2023-03-19T20:57:14Z INFO  librespot] librespot 0.4.2 UNKNOWN (Built on 2023-03-14, Build ID: 315532800, Profile: release)
[2023-03-19T20:57:14Z TRACE librespot] Command line argument(s):
[2023-03-19T20:57:14Z TRACE librespot]      zeroconf-port "5354"
[2023-03-19T20:57:14Z TRACE librespot]      volume-ctrl "linear"
[2023-03-19T20:57:14Z TRACE librespot]      name "Snapcast"
[2023-03-19T20:57:14Z TRACE librespot]      bitrate "320"
[2023-03-19T20:57:14Z TRACE librespot]      backend "pipe"
[2023-03-19T20:57:14Z TRACE librespot]      initial-volume "100"
[2023-03-19T20:57:14Z TRACE librespot]      verbose
[2023-03-19T20:57:14Z DEBUG librespot_discovery::server] Zeroconf server listening on 0.0.0.0:5354
*** WARNING *** The program 'librespot' uses the Apple Bonjour compatibility layer of Avahi.
*** WARNING *** Please fix your application to use the native API of Avahi!
*** WARNING *** For more information see <http://0pointer.de/blog/projects/avahi-compat.html>

Host (what you are running librespot on):

# avahi-daemon --version
avahi-daemon 0.8
vlada-dudr commented 1 year ago

It is due Librespot implementing it's own mDNS stack instead of just registering with Avahi. So they fight each other.

ettom commented 1 year ago

It is due Librespot implementing it's own mDNS stack instead of just registering with Avahi. So they fight each other.

I have compiled librespot with the dns-sd backend. Isn't that supposed to use Avahi for discovery?

vlada-dudr commented 1 year ago

Ok, then it should use avahi. But it probably doesn't retry if avahi is unavailable. One has to correctly order service dependencies in systemd or other supervisor used. Systemd can propagate restarts if configured to do so, but that's only workaround for proper dbus discovery

May 18, 2023 11:42:20 ettom @.***>:

It is due Librespot implementing it's own mDNS stack instead of just registering with Avahi. So they fight each other.

I have compiled librespot with the dns-sd backend. Isn't that supposed to use Avahi for discovery?

— Reply to this email directly, view it on GitHub[https://github.com/librespot-org/librespot/issues/1133#issuecomment-1552802661], or unsubscribe[https://github.com/notifications/unsubscribe-auth/ADD33ZLX55QUFCVXIHDXV4LXGXVHZANCNFSM6AAAAAAWAKUYMM]. You are receiving this because you commented.[Tracking image][https://github.com/notifications/beacon/ADD33ZNGUMX2S7JWPE6JDC3XGXVHZA5CNFSM6AAAAAAWAKUYMOWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTS4RXRWK.gif]

ettom commented 1 year ago

I have set Requires=avahi-daemon.service on the librespot service and it doesn't really work (see my original post). I could try to create a workaround like adding a small delay in ExecStartPre for librespot.service, but I think this should not be required and librespot should handle it internally.

vlada-dudr commented 1 year ago

The problem is though that librespot doesn't listen to events from avahi, so in case of avahi is restarted librespot never applies again for registration. This is caused by librespot using only simple (and deprecated) compatibility bindings to Avahi.

Other issue is that Requires pull dependency in whenever librespot start, but it doesn't order them anyhow. To have librespot started after avahi add After=avahi-daemon.service (or socket might be cleaner). You can also work around the registration issue by using BindsTo=avahi-daemon.service instead Requires , which will start and stop (and restart) librespot in concert with Avahi. The ordering is still done through After.

headblockhead commented 3 months ago

For anyone coming across this in the future, I have found that a similar issue (librespot fully undiscoverable) was resolved for me by enabling publishing / user service publishing in avahi's config.

For some reason this is disabled by default! Took me way too long to figure out :P

In NixOS:

services.avahi = {
  enable = true;
  publish = {
    enable = true;
    userServices = true;
  };
};

Sorry for interrupting / necroposting as this doesn't resolve the issue but this is the (only) top google result for DNS-SD Error: Unknown. and I didn't want to create a new issue for a problem I already fixed myself.

Feel free to hide/delete this post if you want!