openresty / openresty/lua-nginx-module

return unexpected error when ngx.thread.wait() with multipule ngx.thread

Open
#1,439 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

parent thread exec ngx.thread.wait(t1, t2), it only wait for the first thread, without considering other threads it waiting for. This will cause the parent thread beeing waked up by other thread unexpected.
--------------------Bug describing----------------------
for example:
nginx.conf location as follows:
location = /mytest {
allow 127.0.0.1;
deny all;
content_by_lua_file lua/mytest.lua;
}

Then touch lua/mytest.lua, with the follow content:
#cat lua/mytest.lua
function f()
ngx.sleep(0.1)
return "I shouldn't be seen"
end

local t1 = ngx.thread.spawn(f)
if not t1 then
return
end

local t2 = ngx.thread.spawn(f)
if not t2 then
return
end

ok = ngx.thread.wait(t1, t2)
if not ok then
return
end

_, ret = ngx.sleep(3) -- ### expect: sleep 3s, then return: ret is nil
ngx.say(ret)

Then, start nginx-lua, and run # time curl 127.0.0.1:80/mytest
#time curl 127.0.0.1:80/mytest
I shouldn't be seen ## ### ERROR 1: ngx.say(ret) output a unexpected string.

real 0m0.147s ## ### ERROR2: sleep(3) returns quickly, without sleeping 3s
user 0m0.006s
sys 0m0.037s

Error hanppens as ERROR1 and ERROR2.
ERROR 1: ngx.say(ret) output a unexpected string: I shouldn't be seen
ERROR2: sleep(3) returns quickly, without sleeping 3s

what's more, if we replace ngx.sleep with
local sock = ngx.socket.tcp ..... ngx.thread.wait(t1, t2) data = sock.receive
The same errors happen too.

--------------------Bug analysis----------------------
First parent thread stops at ngx.thread.wait(t1, t2);
Then thread t1 wake up the parent thread,
Then parent thread stops at ngx.sleep(3),
Then thread t2 wake up the parent thread,
Then the code ngx.sleep(3) returns quickly, with return value of thread t2!!

--------------------Code analysis----------------------
`
ngx_int_t
ngx_http_lua_run_thread() {
user_co_done:
nrets = lua_gettop(ctx->cur_co_ctx->co);

            next_coctx = ctx->cur_co_ctx->parent_co_ctx;

            if (next_coctx == NULL) {
                /* being a light thread */
                goto no_parent;
            }

            next_co = next_coctx->co;

            /*
             * ended successful, coroutine.resume returns true plus
             * any return values
             */
            lua_pushboolean(next_co, success);

            if (nrets) {
                lua_xmove(ctx->cur_co_ctx->co, next_co, nrets);
            }

            if (ctx->cur_co_ctx->is_uthread) {
                ngx_http_lua_del_thread(r, L, ctx, ctx->cur_co_ctx);
                ctx->uthreads--;
            }

            nrets++;
            ctx->cur_co_ctx = next_coctx;

            ngx_http_lua_probe_info("set parent running");
           // Need TO consider:   clear or modify other threads' coctx->waited_by_parent!!!!
            next_coctx->co_status = NGX_HTTP_LUA_CO_RUNNING;

}
`

Before the last code "next_coctx->co_status = NGX_HTTP_LUA_CO_RUNNING;", we need to :

consider to clear or modify the flags of other threads' coctx->waited_by_parent!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the nginx.conf location and lua/mytest.lua example, then inspect ngx_http_lua_run_thread(), especially the waited_by_parent state when multiple ngx.thread.wait() targets complete. Trace how t1 and t2 resume the parent and verify that the parent remains in ngx.thread.wait() until the requested threads are handled; the sleep(3) call should not be interrupted and the unexpected thread result should not be printed.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.