Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(system): don't forget to patch system.sleep #175

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/copas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ if package.loaded["copas.http"] and (_VERSION=="Lua 5.1") then -- obsolete:
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 +1352,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"].sleep = copas.pause
end

-- yields the current coroutine until explicitly woken up using 'wakeup'
function copas.pauseforever()
Expand Down Expand Up @@ -1524,7 +1532,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
Loading