Player Event causes twice execution of hooks
In case of player event commands in .../hooks/player are executed twice.
Hooks are executed on MPD_PLAYER_IDLE events.
MPD (tested on v0.22.4) sends 4 PLAYER_IDLE events from these functions on track change:
Player SeekDecoder
Player CheckDecoderStartup
Player OpenOutput
PlayerControl LockUpdateSongTag
mpdcron receives only two of them per song change (why?), see debug log (launch with --no-daemon option):
...
Sending idle command with mask 0x8
...
Sending idle command with mask 0x8
Second event is being sent from LockUpdateSongTag function and only if song file is not local (nfs, http, etc.).
https://github.com/MusicPlayerDaemon/MPD/blob/v0.23.7/src/player/Thread.cxx#L790
Perhaps mpdcron can store events into small buffer and ignore duplicates if events are too close?
Here is my lock-based hook solution that will ignore second event. You will need lockfile from procmail package (or just create lock directory and check for it's existence):
#!/usr/bin/env bash
# in-memory lock file to avoid delays during disk write
LOCK_PATH=/dev/shm/player.lock
lockfile -l 0 -s 0 -r 0 "${LOCK_PATH}"
if [[ "$?" -ne 0 ]]; then
rm -f "${LOCK_PATH}"
exit 0
fi
<your code here>
This script is not foolproof and fails from time to time; still better than nothing.