Curl & mpc timing out

Greetings! I’m trying to replicate Dino Fizzotti’s “DiskPlayer” using Mopidy as a back-end rather than spotifyd (which I’ve never been able to get running).

The problem I’m having is that I’m getting timeout errors when I try to add tracks either via mpc or curl - in either case each command sits there for about 30 seconds before giving up. The script is triggered via a udev rule. When I manually run the same curl (or mpc) commands everything works as expected.

The udev rule is as follows:

ACTION=="change", ATTRS{idVendor}=="03ee", ATTRS{idProduct}=="6901", ENV{DISK_MEDIA_CHANGE}=="1", RUN+="/home/pi/media_change.sh $env{DEVNAME}"

The script that runs is as follows:

#!/usr/bin/env bash
exec >> /home/pi/mount.log 2>&1
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8

echo "$(date) Start."
echo "$(date) Media change detected on device $1"

device=${1##*/}

lsblk | grep $device

if [ $? -eq 0 ]; then
    echo "$(date) Device exists on machine."
    echo "$(date) Mounting device $1 to /media/floppy."
    /usr/bin/systemd-mount $1 /media/floppy
    line=$(head -n 1 /media/floppy/diskplayer.contents)
    curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
    curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": { "uris": [ "'$line'" ] } }' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
    curl -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' -H 'Content-Type: application/json' http://localhost:6680/mopidy/rpc
    /usr/bin/systemd-mount --umount /media/floppy
else
    echo "$(date) Device does not exist on machine."
fi
echo "$(date) End."

And this is what gets logged by the script:

Started unit media-floppy.mount for mount point: /media/floppy
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:32 --:--:--     0curl: (7) Failed to connect to localhost port 6680: Connection timed out
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:31 --:--:--     0curl: (7) Failed to connect to localhost port 6680: Connection timed out
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:31 --:--:--     0curl: (7) Failed to connect to localhost port 6680: Connection timed out
Stopped unit media-floppy.mount for mount point: /media/floppy

I get similar timeouts if I try using mpc commands instead - again, about 30 seconds per command before it eventually fails after 90 seconds or so. I’m not entirely sure if it’s really Mopidy-related or not, but I’m not sure what else it could be. Any pointers would be appreciated.

Seems you cannot access the network from a udev context https://askubuntu.com/questions/1166849/18-04-how-can-udev-rule-run-script-access-network

Thanks, that got me pointed in the right direction. Adding IPAddressAllow=127.0.0.1 line in /lib/systemd/system/udev.service will allow udev to access the network. Just to be safe I changed all of the “localhost” references in the script to explicitly point to 127.0.0.1, although that might not have been really necessary.

I also had to force the mopidy service to restart as a part of the disk eject process, otherwise subsequent disk insertions were met with a “connection refused” message for some reason.