Changeset

11761:dbf378dcf27b

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);
author Kim Alvefur <zash@zash.se>
date Tue, 31 Aug 2021 13:34:08 +0200
parents 11760:d66738eeb875
children 11762:54530085dffe
files net/server_epoll.lua
diffstat 1 files changed, 12 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/net/server_epoll.lua	Tue Aug 31 11:38:09 2021 +0200
+++ b/net/server_epoll.lua	Tue Aug 31 13:34:08 2021 +0200
@@ -99,8 +99,14 @@
 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 _, 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 @@
 		peek = timers:peek();
 	end
 
+	if next(closedtimers) ~= nil then
+		closedtimers = {};
+	end
+
 	if peek == nil then
 		return next_delay;
 	else