Skip to content

Commit

Permalink
fix(system): don't forget to patch system.sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Oct 13, 2024
1 parent 14a60e7 commit a33ad87
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/copas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a33ad87

Please sign in to comment.