From f73814b180ad4616ac1d21a729eb9dda4af040cc Mon Sep 17 00:00:00 2001 From: "Avi Halachmi (:avih)" Date: Tue, 22 Jun 2021 15:18:19 +0300 Subject: lua: timers: don't block forever with slow callbacks Previously, process_timers() kept going as long as there were due timers, which could be for extended periods of time or even forever if there were slow timer callbacks with either periodic timers or if timers were added repeatedly. This prevented dequeuing mpv events, and subsequently, among others, prevented mpv from quitting until process_timers() completed. For instance, this caused process_timers() to never return: function render() end mp.add_periodic_timer(1/60, render) Similarly, it never returned if a timer callback always added a new one-shot which was already due by the time the callback completed. This commit ensures that process_timers() only executes callbacks which were due when it started, so that timers which are added (or repeated) during process_timers() will wait for the next iteration - after mpv events are dequeued. This has no performance impact under normal conditions (when callbacks complete before the next timer is due). Additionally, previously idle-observers were executed unconditionally after the timers because indeed there was nothing due when (if...) process_timers() completed. However, now process_timers() can return even if there are due timers, so skip idle-observers on such case. --- player/lua/defaults.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 0fe90977e6..5e56c4fb4e 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -333,6 +333,7 @@ end -- Run timers that have met their deadline. -- Return: next absolute time a timer expires as number, or nil if no timers local function process_timers() + local t0 = nil while true do local timer = get_next_timer() if not timer then @@ -343,6 +344,14 @@ local function process_timers() if wait > 0 then return wait else + if not t0 then + t0 = now -- first due callback: always executes, remember t0 + elseif timer.next_deadline > t0 then + -- don't block forever with slow callbacks and endless timers. + -- we'll continue right after checking mpv events. + return 0 + end + if timer.oneshot then timer:kill() else @@ -518,8 +527,10 @@ function mp.dispatch_events(allow_wait) local wait = 0 if not more_events then wait = process_timers() or 1e20 -- infinity for all practical purposes - for _, handler in ipairs(idle_handlers) do - handler() + if wait ~= 0 then + for _, handler in ipairs(idle_handlers) do + handler() + end end -- Resume playloop - important especially if an error happened while -- suspended, and the error was handled, but no resume was done. -- cgit v1.2.3