Skip to content

Instantly share code, notes, and snippets.

@hamishforbes
Created January 25, 2014 16:43
Show Gist options
  • Save hamishforbes/8619202 to your computer and use it in GitHub Desktop.
Save hamishforbes/8619202 to your computer and use it in GitHub Desktop.
Minimal config to reproduce lua pending timer bug
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
lua_shared_dict test_dict 1m;
keepalive_timeout 65;
server {
listen 80;
location / {
content_by_lua '
-- Construct HTTP request
local http_req = {"GET /foo HTTP/1.1", "Host: localhost:81", "", ""}
http_req = table.concat(http_req, "\\r\\n")
-- Connect the socket
local sock = ngx.socket.tcp()
local ok,err = sock:connect("127.0.0.1", 81)
if not ok then
ngx.log(ngx.ERR, err)
end
-- Send the request
local ok,err = sock:send(http_req)
-- Get Headers
repeat
local line, err = sock:receive("*l")
until string.find(line, "^%s*$")
function foo()
repeat
-- Get and read chunk
local line, err = sock:receive("*l")
local len = tonumber(line)
if len > 0 then
local chunk, err = sock:receive(len)
coroutine.yield(chunk)
sock:receive(2)
else
-- Read last newline
sock:receive(2)
end
until len == 0
end
co = coroutine.create(foo)
repeat
local chunk = select(2,coroutine.resume(co))
until chunk == nil
-- Breaks the timer
sock:setkeepalive()
-- Timer works as expected
--sock:close()
';
log_by_lua '
local background_thread
background_thread = function(premature)
ngx.log(ngx.DEBUG, premature)
if premature then
ngx.shared["test_dict"]:delete("background_flag")
return
end
local ok, err = ngx.timer.at(1, background_thread)
end
local dict = ngx.shared["test_dict"]
if dict:get("background_flag") == nil then
local ok, err = ngx.timer.at(1, background_thread)
if ok then
dict:set("test_dict", 1)
end
end
';
}
}
server {
listen 81;
location / {
echo 'foo';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment