Skip to content

Commit

Permalink
Update worker.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
ADD-SP committed Nov 27, 2023
1 parent 7946d37 commit f3f0674
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/resty/timerng/thread/worker.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local semaphore = require("ngx.semaphore")
local loop = require("resty.timerng.thread.loop")
local utils = require("resty.timerng.utils")

local ngx_log = ngx.log
local ngx_DEBUG = ngx.DEBUG
Expand All @@ -18,6 +19,8 @@ local assert = assert
local string_format = string.format
local setmetatable = setmetatable

local utils_table_clear = utils.table_clear

local CONSTANTS_TOLERANCE_OF_GRACEFUL_SHUTDOWN =
require("resty.timerng.constants").TOLERANCE_OF_GRACEFUL_SHUTDOWN

Expand Down Expand Up @@ -165,6 +168,8 @@ local function thread_body(context, self,
goto continue
end

utils_table_clear(ngx.ctx)

report_before_job_execute_callback(self, job)
job:execute()
report_after_job_execute_callback(self, job)
Expand Down Expand Up @@ -381,4 +386,4 @@ function _M.new(timer_sys, min_threads, max_threads)
end


return _M
return _M
47 changes: 47 additions & 0 deletions spec/07-bugs_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,51 @@ insulate("bugs of every timer | ", function ()

assert.is_false(flag)
end)
end)

insulate("should not share ngx.ctx across different timers", function ()
local timer = { }

randomize()

lazy_setup(function ()
timer = timer_module.new({
min_threads = 1,
max_threads = 1,
})

assert(timer:start())
end)

lazy_teardown(function ()
timer:freeze()
timer:destroy()

helper.wait_until(function ()
assert.same(1, timer_running_count())
return true
end)

end)

before_each(function ()
update_time()
end)

it("", function ()
local flag = false
assert(timer:at(0, function ()
ngx.ctx.a = 1
flag = true
end))

ngx.update_time()
ngx.sleep(1)

assert.is_true(flag)

assert(timer:at(0, function ()
assert.is_nil(ngx.ctx.a)
end))
end)
end)

0 comments on commit f3f0674

Please sign in to comment.