Skip to content

Commit

Permalink
fix(*): use require implementation written in Lua
Browse files Browse the repository at this point in the history
Some of the libraries that we use perform socket functions while they
are being required().  This causes problems if the initial require() of
the module is done from running code (instead of on the module global
level) as require is implemented in C, and yielding is not possible
across the boundaries of a FFI invocation.

This commit replaces the require() implementation by one that is
written in Lua so that yielding is possible on the module global
level.
  • Loading branch information
hanshuebner committed Sep 20, 2023
1 parent 94fbf7e commit fab27cc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG/unreleased/kong/11610.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 0 additions & 8 deletions bin/busted
Original file line number Diff line number Diff line change
Expand Up @@ -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 })

Expand Down
1 change: 1 addition & 0 deletions kong-3.5.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
File renamed without changes.

0 comments on commit fab27cc

Please sign in to comment.