diff --git a/CHANGELOG/unreleased/kong/11610.yaml b/CHANGELOG/unreleased/kong/11610.yaml new file mode 100644 index 000000000000..b55a604e994a --- /dev/null +++ b/CHANGELOG/unreleased/kong/11610.yaml @@ -0,0 +1,7 @@ +"message": "Use `require` implementation written in Lua to enable cosocket calls on the module level of libraries" +"type": "feature" +"scope": "Core" +"prs": + - 11610 +jiras: + - "KAG-2595" diff --git a/bin/busted b/bin/busted index e676a55c3acf..dfc41fec1230 100755 --- a/bin/busted +++ b/bin/busted @@ -59,14 +59,6 @@ require("kong.globalpatches")({ rbusted = true }) --- some libraries used in test like spec/helpers --- calls cosocket in module level, and as LuaJIT's --- `require` is implemented in C, this throws --- "attempt to yield across C-call boundary" error --- the following pure-lua implementation is to bypass --- this limitation, without need to modify all tests -_G.require = require "spec.require".require - -- Busted command-line runner require 'busted.runner'({ standalone = false }) diff --git a/kong-3.5.0-0.rockspec b/kong-3.5.0-0.rockspec index 8c59cf2906b6..141fd91658ca 100644 --- a/kong-3.5.0-0.rockspec +++ b/kong-3.5.0-0.rockspec @@ -157,6 +157,7 @@ build = { ["kong.tools.stream_api"] = "kong/tools/stream_api.lua", ["kong.tools.queue"] = "kong/tools/queue.lua", ["kong.tools.queue_schema"] = "kong/tools/queue_schema.lua", + ["kong.tools.require"] = "kong/tools/require.lua", ["kong.tools.sandbox"] = "kong/tools/sandbox.lua", ["kong.tools.uri"] = "kong/tools/uri.lua", ["kong.tools.kong-lua-sandbox"] = "kong/tools/kong-lua-sandbox.lua", diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 7d90cc1c9354..990f53ab2407 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -583,6 +583,14 @@ return function(options) return sock end + -- Some libraries call cosocket on the module level, and as + -- LuaJIT's `require` is implemented in C, this throws "attempt to + -- yield across C-call boundary" error. We're replacing require + -- with an implementation written in Lua that does not have the + -- restriction. + _G.native_require = require + _G.require = require "kong.tools.require".require + -- STEP 5: load code that should be using the patched versions, if any (because of dependency chain) do local client = package.loaded["kong.resty.dns.client"] diff --git a/spec/require.lua b/kong/tools/require.lua similarity index 100% rename from spec/require.lua rename to kong/tools/require.lua