JakeStanger / mpd-discord-rpc

Rust application which displays your currently playing song / album / artist from MPD in Discord using Rich Presence.
https://crates.io/crates/mpd-discord-rpc
MIT License
88 stars 16 forks source link

Extremely high CPU usage attempting to connect to Discord when it's not running #137

Closed 00dani closed 1 month ago

00dani commented 1 month ago

I'm on MacOS 14.5 and I've set up mpd-discord-rpc as a launchd service running at startup. This works just fine when DIscord is open. However, if Discord isn't running when mpd-discord-rpc is launched, then mpd-discord-rpc rapidly attempts to connect to it over and over, consuming significant CPU in the process.

2024-06-13T02:03:17.016796Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })
2024-06-13T02:03:17.016859Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })
2024-06-13T02:03:17.016924Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })

I've done some quick digging into the discord-presence crate and found that the "No such file or directory" message is referring to the Unix socket $XDG_RUNTIME_DIR/discord-ipc-0, which indeed does not exist until Discord is launched.

However, from what I can gather, the discord-presence crate is written to expect this situation, with the ability to wait between connection attempts as well as give up after a configurable number of attempts - by default it waits for five seconds between attempts. As far as I can tell, mpd-discord-rpc does not reconfigure this behaviour, but it's definitely not waiting five seconds between attempts.

Is this a weird interaction between discord-presence using std::thread and mpd-discord-rpc using non-blocking async through Tokio, maybe? The "wait before trying again" behaviour in discord-presence is implemented as a call to std::thread::sleep, and I don't really know what happens if you try to call that from a Tokio application.

JakeStanger commented 1 month ago

Can you confirm which version of mpd-discord-rpc you're running? I've got a feeling this was an issue that was fixed, although I may be mis-remembering.

00dani commented 1 month ago

Just checked with cargo install --list, I'm on mpd-discord-rpc v1.7.2, which is the latest release. 🤔

JakeStanger commented 1 month ago

Hm strange, I've not been able to replicate this on 1.7.2 or the latest git build. I'm wondering if it's perhaps related to the way threads are handled on MacOS vs Linux. Unfortunately I don't have a way of trying that myself.

That said, a potentially related update to the discord-presence crate has been put out which 1.7.2 doesn't include (unless Cargo feels like resolving it, which might be what's happening for me...). I've got the update on Git so I'll stick out a new release and see.

JakeStanger commented 1 month ago

New release out - let me know if that helps

00dani commented 1 month ago

Ran a quick cargo install mpd-discord-rpc, I'm now on v1.7.3 - and it did! It's now waiting a sensible five seconds between attempts and barely produces a blip on my CPU, as it ought to be when there's no Discord to connect to:

2024-06-14T01:24:53.856858Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })
2024-06-14T01:24:58.857060Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })
2024-06-14T01:25:03.857253Z ERROR discord_presence::connection::manager: Failed to connect: IoError(Os { code: 2, kind: NotFound, message: "No such file or directory" })

Thank you for checking this out!