From a33ad877a6f7e3f8a3b4b8def45eec94bb0b32f8 Mon Sep 17 00:00:00 2001 From: Thijs Schreijer Date: Sun, 13 Oct 2024 10:17:40 +0200 Subject: [PATCH] fix(system): don't forget to patch system.sleep --- src/copas.lua | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/copas.lua b/src/copas.lua index 77699d3..f79ec0d 100644 --- a/src/copas.lua +++ b/src/copas.lua @@ -19,19 +19,26 @@ end if package.loaded["copas.http"] and (_VERSION=="Lua 5.1") then -- obsolete: only for Lua 5.1 compatibility error("you must require copas before require'ing copas.http") end +if package.loaded["system"] then -- required to be able to patch system.sleep + error("you must require copas before require'ing system") +end -- load either LuaSocket, or LuaSystem -local socket, system do +-- note: with luasocket we don't use 'sleep' but 'select' with no sockets +local socket, system, system_sleep do if pcall(require, "socket") then -- found LuaSocket socket = require "socket" - else - -- fallback to LuaSystem - if pcall(require, "system") then - system = require "system" - else - error("Neither LuaSocket nor LuaSystem found, Copas requires at least one of them") - end + end + + -- try LuaSystem as fallback + if pcall(require, "system") then + system = require "system" + system_sleep = system.sleep -- system.sleep will be patched later on + end + + if not (socket or system) then + error("Neither LuaSocket nor LuaSystem found, Copas requires at least one of them") end end @@ -1348,6 +1355,10 @@ function copas.pause(sleeptime) end end +-- patch luasystem to use copas.pause instead of system_sleep +if package.loaded["system"] then + package.loaded["system"] = copas.pause +end -- yields the current coroutine until explicitly woken up using 'wakeup' function copas.pauseforever() @@ -1524,7 +1535,7 @@ local _select_plain do if not socket then -- socket module unavailable, switch to luasystem sleep - _select_plain = system.sleep + _select_plain = system_sleep else -- use socket.select to handle socket-io _select_plain = function(timeout)