# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1630409648 -7200
# Node ID dbf378dcf27b8745c4c04257a951e023a705759a
# Parent  d66738eeb87595bec8391fb9fe73470fead62502
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);

diff -r d66738eeb875 -r dbf378dcf27b net/server_epoll.lua
--- 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