apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: limit-count throws error casually in apisix 2.0

Open JanLi-air opened this issue 6 years ago • 8 comments

Issue description

When configured limit-count plugin in global rule, it sometimes throws error and respond with 500.

Environment

  • apisix version (cmd: apisix version): 2.0
  • OS: Linux

Minimal test code / Steps to reproduce the issue

  1. Configure limit-count in global rule like this:
"limit-count": {
        "count": 1000000000,
        "policy": "local",
        "key": "remote_addr",
        "rejected_code": 429,
        "time_window": 1
}
  1. Repeatly request apisix
  2. The error happens after some requests, might need thousands of requests to reproduce the issue.

What's the actual result? (including assertion message & call stack if applicable)

The request got 500 response code due to runtime error in limit-count.lua:

2020/11/11 09:48:15 [error] 99#99: *239834 lua entry thread aborted: runtime error: ...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua:167: attempt to call method 'incoming' (a nil value)
stack traceback:
coroutine 0:
 ...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua: in function 'phase_func'
 /usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:141: in function 'run_plugin'
 /usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:350: in function 'http_access_phase'
 access_by_lua(nginx.conf:225):2: in main chunk, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"

What's the expected result?

The response code should be 200 for normal requests, it should only be 429 when the limit count is reached.

JanLi-air avatar Nov 11 '20 10:11 JanLi-air

The problem might be limit-count fetched incorrect limit object from lru cache.

JanLi-air avatar Nov 11 '20 10:11 JanLi-air

@JanLi-air Could you add this code to see what happened when limit object is incorrect?

    if not lim.incoming then
         for k, v in pairs(lim) do
            core.log.error("lim:", k, " ", core.json.delay_encode(v))
        end
    end

    local delay, remaining = lim:incoming(key, true)

spacewander avatar Nov 11 '20 11:11 spacewander

And have you tried the master branch? The lru cache is changed after 2.0 and some issues are addressed. Not sure if your problem is solved.

spacewander avatar Nov 11 '20 12:11 spacewander

2020/11/12 02:21:31 [error] 126#126: *3929332 [lua] limit-count.lua:169: phase_func(): lim:ver 2935, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [warn] 126#126: *3929332 [lua] json.lua:82: failed to encode: Cannot serialise userdata: type not supported force: nil, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [warn] 126#126: *3929332 [lua] json.lua:82: failed to encode: Cannot serialise userdata: type not supported force: nil, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [error] 126#126: *3929332 [lua] limit-count.lua:169: phase_func(): lim:val , client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [error] 126#126: *3929332 lua entry thread aborted: runtime error: ...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua:173: attempt to call method 'incoming' (a nil value)
stack traceback:
coroutine 0:
	...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua: in function 'phase_func'
	/usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:141: in function 'run_plugin'
	/usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:350: in function 'http_access_phase'
	access_by_lua(nginx.conf:225):2: in main chunk, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [error] 124#124: *3929368 [lua] limit-count.lua:169: phase_func(): lim:ver 2935, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [warn] 124#124: *3929368 [lua] json.lua:82: failed to encode: Cannot serialise userdata: type not supported force: nil, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [warn] 124#124: *3929368 [lua] json.lua:82: failed to encode: Cannot serialise userdata: type not supported force: nil, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [error] 124#124: *3929368 [lua] limit-count.lua:169: phase_func(): lim:val , client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"
2020/11/12 02:21:31 [error] 124#124: *3929368 lua entry thread aborted: runtime error: ...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua:173: attempt to call method 'incoming' (a nil value)
stack traceback:
coroutine 0:
	...esty/luajit/share/lua/5.1/apisix/plugins/limit-count.lua: in function 'phase_func'
	/usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:141: in function 'run_plugin'
	/usr/local/openresty/luajit/share/lua/5.1/apisix/init.lua:350: in function 'http_access_phase'
	access_by_lua(nginx.conf:225):2: in main chunk, client: 172.16.128.215, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "10.212.4.241:8080"

JanLi-air avatar Nov 12 '20 02:11 JanLi-air

this is what I got, what is this value lim:ver 2935?

JanLi-air avatar Nov 12 '20 02:11 JanLi-air

And have you tried the master branch? The lru cache is changed after 2.0 and some issues are addressed. Not sure if your problem is solved.

What are those issues? Can you share the links?

JanLi-air avatar Nov 12 '20 02:11 JanLi-air

And have you tried the master branch? The lru cache is changed after 2.0 and some issues are addressed. Not sure if your problem is solved.

What are those issues? Can you share the links?

https://github.com/apache/apisix/pull/2575/files

spacewander avatar Nov 12 '20 02:11 spacewander

I think your issue is fixed. See https://github.com/apache/apisix/pull/2575/files#r521784444 about why it is fixed. Feel free to reopen it if you still reproduce the issue with the master branch.

spacewander avatar Nov 12 '20 02:11 spacewander