Skip to content

Incorrect result for a special lua script, if jit enabled #22

@qleein

Description

@qleein

At begining, everything is OK, but after dozens of requests, I got wrong result。
If I disable the jit by jit.off(), it works well

Software Environment

os: centos6
gcc:  gcc-4.9.3
nginx: nginx-1.13.12
luajit:  luajit-2.1.0(v2.1-agentzh branch)
lua-nginx-module: v0.10.13
lua-resty-core: v0.1.13

nginx -V

# ./nginx -V
nginx version: nginx/1.13.12
built by gcc 4.9.3 (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-debug --with-cc-opt='-g -O0' --with-ld-opt=-Wl,-rpath=/usr/local/lib --with-pcre=/root/Nginx/pcre-8.40 --with-zlib=/root/Nginx/zlib-1.2.11 --add-module=/root/Nginx/lua-nginx-module

nginx config file

daemon off;
master_process off;
error_log logs/error.log error;

events {
    use epoll;
}

http {
    lua_package_path "lib/?.lua;;";
    init_by_lua_block {
        g_test = {status = "", count = 0}
    }
    server {
        listen 127.0.0.1:80;
        location / {
            set $x_value "0";

            access_by_lua_block {
                --jit.off()
                require "resty.core.var"
                require "resty.core.response"

                local control = require "control"
                control.access_handler()
            }
            log_by_lua_block {
                g_test.count = g_test.count + 1
                ngx.log(ngx.ERR, "req_count: ", g_test.count, ", status: ", g_test.status, ", value: ", ngx.var.x_value)
            }
    
            content_by_lua_block {
                ngx.say("method: ", ngx.var.request_method)
                ngx.say("request_uri: ", ngx.var.request_uri)
                ngx.say("value: ", ngx.var.x_value)
            }
        }
    }
}

file control.lua(a lua module)

local function dummy(mconf) return nil end

local function handler()
    g_test.status = "init"
    local mconf = {
        { path = "/", value = 180 }, { path = "/api/", value = 60 }
    }

	local t = {}
    for _, v in ipairs(mconf) do
        local pos = #t + 1
        while pos > 1 do
            if v["path"] == "/" then break end
            pos = pos - 1
        end

        table.insert(t, pos, v)
    end

    local value = 0
    local url = ngx.var["uri"]
    for _, rule in ipairs(t) do
        if string.find(url, rule["path"], 1, true) then
            g_test.status = g_test.status .. " + matched"
            value = rule["value"]
            break
        end
    end

    if value <= 0 then
        g_test.status = g_test.status .. " + noValue"
        return
    end

    ngx.var["x_value"] = value
    ngx.header["X-header"] = value
    return
end

return {
    access_handler = handler
}

visit nginx in ternimal

$ while true; do curl 127.0.0.1/; done

Corrent result as bellow

$ curl 127.0.0.1
method: GET
request_uri: /
value: 180

Incorrect result after visit nginx 34 times

[root@SHTELVM-001 nginx]# curl 127.0.0.1
method: GET
request_uri: /
value: 0

nginx error log as bellow, after 34th request, rule matched but value not assgin a proper value in control.lua, line 25.

2018/05/09 15:44:43 [error] 14447#0: *1 [lua] log_by_lua(nginx.conf:30):3: req_count: 1, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *2 [lua] log_by_lua(nginx.conf:30):3: req_count: 2, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *3 [lua] log_by_lua(nginx.conf:30):3: req_count: 3, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *4 [lua] log_by_lua(nginx.conf:30):3: req_count: 4, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *5 [lua] log_by_lua(nginx.conf:30):3: req_count: 5, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *6 [lua] log_by_lua(nginx.conf:30):3: req_count: 6, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *7 [lua] log_by_lua(nginx.conf:30):3: req_count: 7, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:43 [error] 14447#0: *8 [lua] log_by_lua(nginx.conf:30):3: req_count: 8, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *9 [lua] log_by_lua(nginx.conf:30):3: req_count: 9, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *10 [lua] log_by_lua(nginx.conf:30):3: req_count: 10, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *11 [lua] log_by_lua(nginx.conf:30):3: req_count: 11, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *12 [lua] log_by_lua(nginx.conf:30):3: req_count: 12, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *13 [lua] log_by_lua(nginx.conf:30):3: req_count: 13, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *14 [lua] log_by_lua(nginx.conf:30):3: req_count: 14, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *15 [lua] log_by_lua(nginx.conf:30):3: req_count: 15, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *16 [lua] log_by_lua(nginx.conf:30):3: req_count: 16, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:44 [error] 14447#0: *17 [lua] log_by_lua(nginx.conf:30):3: req_count: 17, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *18 [lua] log_by_lua(nginx.conf:30):3: req_count: 18, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *19 [lua] log_by_lua(nginx.conf:30):3: req_count: 19, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *20 [lua] log_by_lua(nginx.conf:30):3: req_count: 20, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *21 [lua] log_by_lua(nginx.conf:30):3: req_count: 21, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *22 [lua] log_by_lua(nginx.conf:30):3: req_count: 22, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *23 [lua] log_by_lua(nginx.conf:30):3: req_count: 23, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *24 [lua] log_by_lua(nginx.conf:30):3: req_count: 24, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *25 [lua] log_by_lua(nginx.conf:30):3: req_count: 25, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *26 [lua] log_by_lua(nginx.conf:30):3: req_count: 26, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:45 [error] 14447#0: *27 [lua] log_by_lua(nginx.conf:30):3: req_count: 27, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *28 [lua] log_by_lua(nginx.conf:30):3: req_count: 28, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *29 [lua] log_by_lua(nginx.conf:30):3: req_count: 29, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *30 [lua] log_by_lua(nginx.conf:30):3: req_count: 30, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *31 [lua] log_by_lua(nginx.conf:30):3: req_count: 31, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *32 [lua] log_by_lua(nginx.conf:30):3: req_count: 32, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *33 [lua] log_by_lua(nginx.conf:30):3: req_count: 33, status: init + matched, value: 180 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *34 [lua] log_by_lua(nginx.conf:30):3: req_count: 34, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *35 [lua] log_by_lua(nginx.conf:30):3: req_count: 35, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:46 [error] 14447#0: *36 [lua] log_by_lua(nginx.conf:30):3: req_count: 36, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:47 [error] 14447#0: *37 [lua] log_by_lua(nginx.conf:30):3: req_count: 37, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:47 [error] 14447#0: *38 [lua] log_by_lua(nginx.conf:30):3: req_count: 38, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:47 [error] 14447#0: *39 [lua] log_by_lua(nginx.conf:30):3: req_count: 39, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"
2018/05/09 15:44:47 [error] 14447#0: *40 [lua] log_by_lua(nginx.conf:30):3: req_count: 40, status: init + matched + noValue, value: 0 while logging request, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions