Changeset

11262:2c559953ad41 0.11

net.server_epoll: Ensure timers can't run more than once per tick This makes sure that a timer that returns 0 (or less) does not prevent runtimers() from completing, as well as making sure a timer added with zero timeout from within a timer does not run until the next tick. Thanks tmolitor
author Kim Alvefur <zash@zash.se>
date Fri, 08 Jan 2021 21:57:19 +0100
parents 11261:be38ae8fdfa5
children 11263:1274deeab39a
files net/server_epoll.lua
diffstat 1 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/net/server_epoll.lua	Thu Dec 24 17:57:28 2020 +0100
+++ b/net/server_epoll.lua	Fri Jan 08 21:57:19 2021 +0100
@@ -97,10 +97,10 @@
 	-- Any timers at all?
 	local now = gettime();
 	local peek = timers:peek();
+	local readd;
 	while peek do
 
 		if peek > now then
-			next_delay = peek - now;
 			break;
 		end
 
@@ -109,13 +109,29 @@
 		if ok and type(ret) == "number"  then
 			local next_time = now+ret;
 			timer[1] = next_time;
-			timers:insert(timer, next_time);
+			-- Delay insertion of timers to be re-added
+			-- so they don't get called again this tick
+			if readd then
+				readd[id] = timer;
+			else
+				readd = { [id] = timer };
+			end
 		end
 
 		peek = timers:peek();
 	end
+
+	if readd then
+		for _, timer in pairs(readd) do
+			timers:insert(timer, timer[2]);
+		end
+		peek = timers:peek();
+	end
+
 	if peek == nil then
 		return next_delay;
+	else
+		next_delay = peek - now;
 	end
 
 	if next_delay < min_wait then