iterate over table

184 views
Skip to first unread message

Hossein Vadizadeh

unread,
Aug 14, 2022, 8:54:24 PM8/14/22
to openresty-en
i am testing my proxy server by new version of openresty (1.21.4), and i have a big problem
in load test
my nginx config just :worker 1;
and i have some code like this:
-----------------------------------------------------------------------------
rewrite_by_lua_block {
    local t = {["1.2.3.4"] = { ["weight"] = 1}, ["4.5.6.7"] = { ["weight"] = 2}}
    ngx.ctx.t = t
}

balancer_by_lua_block{
    localt = ngx.ctx.t
    for key, value in pairs(t) do print(key) end -- output is empty
    for key, value in pairs(t) do print(key) end -- have output
}
-----------------------------------------------------------------------------


and you can see, at the first iterate my table is not iterable (and is not nil), but at the second iterate after that, its work, really confusing, do you have any idea?

Gene Unigovski

unread,
Aug 24, 2022, 8:03:32 AM8/24/22
to openresty-en
...interesting ...have you be able to find what is going on?...

Yichun Zhang

unread,
Aug 31, 2022, 3:41:51 PM8/31/22
to openresty-en
Hello!

Thanks for your report!

I tried your test case on my side (though I had to fix a typo in your
Lua code and also add more stuff to make it runnable) and cannot
reproduce the problem using the same openresty version on x86_64
Linux. Below is my test case using OpenResty's test scaffold
Test::Nginx::Socket::Lua (this way, everyone can easily run it on
their side):

use Test::Nginx::Socket::Lua 'no_plan';

repeat_each(300);

run_tests;

__DATA__

=== TEST 1: test case from community
--- http_config
upstream self {
server 127.0.0.1:$TEST_NGINX_SERVER_PORT;
balancer_by_lua_block {
local t = ngx.ctx.t
for key, value in pairs(t) do print(key) end -- output is empty
for key, value in pairs(t) do print(key) end -- have output
}
}

--- config
location = /t {
rewrite_by_lua_block {
local t = {["1.2.3.4"] = { ["weight"] = 1}, ["4.5.6.7"] =
{ ["weight"] = 2}}
ngx.ctx.t = t
}

proxy_pass http://self/;
}
--- request
GET /t
--- response_body_like chop
It works
--- error_log
4.5.6.7
1.2.3.4
--- ONLY

I added repeat_each(300); near the beginning of the .t test file so
that it can trigger LuaJIT's JIT compiling. And all the test runs were
completed successfully. And to count the output lines in the nginx
error.log file:

$ grep '4.5.6.7 while connecting to upstream' t/servroot/logs/error.log|wc -l
600

$ grep '1.2.3.4 while connecting to upstream' t/servroot/logs/error.log|wc -l
600

We can see that we didn't miss a single line of key output. It is
indeed 2 * 300 == 600 lines for each key value.

I also tried commenting out the 2nd "for" loop statement on my side
and this time it still produces the expected output, just half of the
key values are printed in the error.log file:

$ grep '4.5.6.7 while connecting to upstream' t/servroot/logs/error.log|wc -l
300

$ grep '1.2.3.4 while connecting to upstream' t/servroot/logs/error.log|wc -l
300

Will you prepare a minimal and standalone example that can help us
reproduce the problem on our side? Or am I missing something here?

Thanks!

Best,
Yichun

Yichun Zhang

unread,
Sep 14, 2022, 7:05:58 PM9/14/22
to openresty-en
Hi there,

On Wed, Aug 31, 2022 at 12:41 PM Yichun Zhang <yic...@openresty.com> wrote:
> for key, value in pairs(t) do print(key) end -- output is empty
> for key, value in pairs(t) do print(key) end -- have output

We recently hit and reproduced a similar issue in one of our in-house
Lua applications (OpenResty XRay). I dug it up (thanks to rr and my
colleague, Jiahao Wang) and reported it to Mike Pall with enough
details for him to fix it upstream:

https://github.com/LuaJIT/LuaJIT/commit/dad04f17547

Mike also kindly provided a minimal reproducer:

https://github.com/openresty/luajit2-test-suite/blob/master/test/misc/jloop-itern-stack-check-fix.lua

I'm not sure if it's related since I couldn't reproduce any issues in
the original balancer_by_lua example from Hossein Vadizadeh. Hossein:
will you please try out this LuaJIT patch on your side?

Thanks!

Best,
Yichun
Reply all
Reply to author
Forward
0 new messages