openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.33k stars 2.03k forks source link

ngx.run_worker_thread doesn't work outside of content_by_lua*? #2122

Open travisbell opened 2 years ago

travisbell commented 2 years ago

Hi guys,

I'm not clear if this is just a documentation issue, or an actual bug. The docs for ngx.run_worker_thread state that it can work inside of rewrite_by_lua*, access_by_lua* and content_by_lua*. Based on my testing just now, it seems as though it actually only works within content_by_lua*.

When you try and use it inside of the other two phases, the function fires but then the request hangs indefinitely and never completes. No error or anything is reported, it just hangs forever.

nginx.conf

server {
  set $cache_key "";

  location /test {
    rewrite_by_lua_block {
      local ok, md5_or_err = ngx.run_worker_thread("default", "utils", "md5")
      if not ok then
        ngx.say(ok, " : ", md5_or_err)
        return
      end
      ngx.var.cache_key = md5_or_err
    }
  }
}

utils.lua

local function md5()
    return ngx.md5("hello")
end

return { md5 = md5 }

However, run the same block within a content_by_lua_block, and everything executes as expected.

So what is it? A documentation issue, or should ngx.run_worker_thread work within these extra phases? This is on OR 1.21.4.1 by the way.

zhuizhuhaomeng commented 2 years ago

If you are using openresty-1.21.4, then there is a bug. This bug has been fix in the latest master branch.

travisbell commented 2 years ago

Thanks @zhuizhuhaomeng, guess I'll either wait for a new release to be cut or try custom compiling the latest lua-nginx-module.