From 307e58cc75eda7aa9b7bef4d65d7952b6bca8748 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Tue, 31 Aug 2021 13:34:08 +0200 Subject: net.server_epoll: Prevent removed timers from being readded In a case like this the timer would not be readded: addtimer(1, function(t, id) stop(id) return 1 end); --- net/server_epoll.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'net') diff --git a/net/server_epoll.lua b/net/server_epoll.lua index fa8a482d..2841278d 100644 --- a/net/server_epoll.lua +++ b/net/server_epoll.lua @@ -99,8 +99,14 @@ local fds = createtable(10, 0); -- FD -> conn local timers = indexedbheap.create(); local function noop() end + +-- Keep track of recently closed timers to avoid re-adding them +local closedtimers = {}; + local function closetimer(id) - timers:remove(id); + if timers:remove(id) then + closedtimers[id] = true; + end end local function reschedule(id, time) @@ -138,7 +144,7 @@ local function runtimers(next_delay, min_wait) local _, timer, id = timers:pop(); local ok, ret = xpcall(timer, traceback, now, id); - if ok and type(ret) == "number" then + if ok and type(ret) == "number" and not closedtimers[id] then local next_time = elapsed+ret; -- Delay insertion of timers to be re-added -- so they don't get called again this tick @@ -161,6 +167,10 @@ local function runtimers(next_delay, min_wait) peek = timers:peek(); end + if next(closedtimers) ~= nil then + closedtimers = {}; + end + if peek == nil then return next_delay; else -- cgit v1.2.3