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(vault): vault try should avoid using semaphore in non-yieldable phases #11617

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions kong/concurrency.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
local resty_lock = require "resty.lock"
local ngx_semaphore = require "ngx.semaphore"
local in_yieldable_phase = require("kong.tools.utils").in_yieldable_phase


local type = type
local error = error
local pcall = pcall


local get_phase = ngx.get_phase


local concurrency = {}


Expand Down Expand Up @@ -91,7 +89,7 @@ function concurrency.with_coroutine_mutex(opts, fn)
error("invalid value for opts.on_timeout", 2)
end

if get_phase() == "init_worker" then
if not in_yieldable_phase() then
return fn()
end

Expand Down
29 changes: 24 additions & 5 deletions kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1648,26 +1648,45 @@ function _M.sort_by_handler_priority(a, b)
return prio_a > prio_b
end

do
---
-- Check if the phase is yieldable.
-- @tparam string phase the phase to check, if not specified then
-- the default value will be the current phase
-- @treturn boolean true if the phase is yieldable, false otherwise
local in_yieldable_phase do
local get_phase = ngx.get_phase
local ngx_sleep = _G.native_ngx_sleep or ngx.sleep

local SLEEP_PHASES = {
-- https://github.com/openresty/lua-nginx-module/blob/c89469e920713d17d703a5f3736c9335edac22bf/src/ngx_http_lua_util.h#L35C10-L35C10
local LUA_CONTEXT_YIELDABLE_PHASE = {
rewrite = true,
server_rewrite = true,
access = true,
content = true,
timer = true,
ssl_client_hello = true,
ssl_certificate = true,
ssl_session_fetch = true,
ssl_client_hello = true,
preread = true,
}

in_yieldable_phase = function(phase)
if LUA_CONTEXT_YIELDABLE_PHASE[phase or get_phase()] == nil then
return false
end
return true
end
end

_M.in_yieldable_phase = in_yieldable_phase

do
local ngx_sleep = _G.native_ngx_sleep or ngx.sleep

local YIELD_ITERATIONS = 1000
local counter = YIELD_ITERATIONS

function _M.yield(in_loop, phase)
if ngx.IS_CLI or SLEEP_PHASES[phase or get_phase()] == nil then
if ngx.IS_CLI or not in_yieldable_phase(phase) then
return
end
if in_loop then
Expand Down
22 changes: 22 additions & 0 deletions spec/02-integration/13-vaults/03-mock_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,26 @@ for _, strategy in helpers.each_strategy() do
end)
end)
end)

if strategy == "postgres" then
describe("ENV Vault #" .. strategy, function ()
describe("Kong Start", function ()
it("can resolve reference in init_phase", function ()
helpers.setenv("TEST_ENV_VAULT_LOGLEVEL", "debug")

assert(helpers.start_kong {
database = strategy,
prefix = helpers.test_conf.prefix,
nginx_conf = "spec/fixtures/custom_nginx.template",
vaults = "env",
log_level = "{vault://env/TEST_ENV_VAULT_LOGLEVEL}"
})

finally(function ()
assert(helpers.stop_kong())
end)
end)
end)
end)
end
end