Fix bug with dangling timers

In certain cases, usually around loss of connection to the network, it
was possible for the MPD connection to be dropped while *mpd-timer*
was still active and scheduled. This led to mpd-ping being called
on the timer every *mpd-timeout* seconds, and uselessly popping up an
error message.

This fixes that bug by replacing the simple mpd-ping call in the timer
with a lambda that checks for *mpd-socket* and, if not connected,
cancels the timer. It also makes mpd-disconnect cancel the timer
outside the with-mpd-connection form, which means that in the case of
a dangling timer mpd-disconnect will still cancel it.
This commit is contained in:
Tom Hunt 2022-07-13 11:55:57 -06:00
parent b33f01a2f1
commit 66d672e2d3

View file

@ -116,7 +116,8 @@
(message "Error with mpd connection: ~a" c)
(setf *mpd-socket* nil)
(when *mpd-timer*
(cancel-timer *mpd-timer*)))))
(cancel-timer *mpd-timer*)
(setf *mpd-timer* nil)))))
(message "Error: not connected to mpd")))
(defun mpd-send (command)
@ -204,7 +205,13 @@
(when *mpd-socket*
(when *mpd-timeout*
(setf *mpd-timer*
(run-with-timer *mpd-timeout* *mpd-timeout* 'mpd-ping)))
(run-with-timer *mpd-timeout* *mpd-timeout*
(lambda ()
(if *mpd-socket*
(mpd-ping)
(when *mpd-timer*
(cancel-timer *mpd-timer*)
(setf *mpd-timer* nil)))))))
(mpd-receive t)
(when *mpd-password*
(mpd-format-command "password \"~a\"" *mpd-password*))))
@ -681,10 +688,12 @@ Volume
(defcommand mpd-disconnect () ()
"Disconnect from mpd server"
(when *mpd-timer*
(cancel-timer *mpd-timer*)
(setf *mpd-timer* nil))
(with-mpd-connection
(close *mpd-socket*)
(setf *mpd-socket* nil)
(when *mpd-timer* (cancel-timer *mpd-timer*))))
(setf *mpd-socket* nil)))
(defcommand mpd-kill () ()
(mpd-send-command "kill"))