openresty / lua-nginx-module

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

runtime error: no request ctx found #1350

Open hongxiaolong opened 6 years ago

hongxiaolong commented 6 years ago

Sometimes, I got error logs as follows:

2018/07/04 15:11:11 [error] 91328#0: *1 lua entry thread aborted: runtime error: rewrite_by_lua(nginx.conf:49):5: no request ctx found
stack traceback:
coroutine 0:
        [C]: in function 'say'
        rewrite_by_lua(nginx.conf:49):5: in function <rewrite_by_lua(nginx.conf:49):1>, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8088"

or

pcall(main) failed: xxx
 API disabled in the context of (unknown) while sending to client, 

I found that in some conditions like:

    server {
        listen       8088;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            rewrite_by_lua '
                local res = ngx.location.capture("@get")
                ngx.header["Last-Modified"] = "Sat, 30 Jun 2019 10:30:51 GMT"
                ngx.say("will trigger API disabled")
                ngx.say("will trigger API disabled")
            ';
        }

        location @get {
            set $ups http://localhost:8082/get_ups;
            proxy_pass $ups;
            add_header "Last-Modified" "Sat, 30 Jun 2019 10:30:51 GMT";
        }

        location /get_ups {
            return 200 "OKK";
            add_header "Last-Modified" "Sat, 30 Jun 2019 10:30:51 GMT";
        }

}

Command like:

curl localhost:8088 -H 'if-unmodified-since: Sat, 30 Jun 2017 18:30:50 GMT' -i

Version: openresty-1.13.6.2

Because in ngx_http_not_modified_filter_module.c r was finalized

static ngx_int_t
ngx_http_not_modified_header_filter(ngx_http_request_t *r)
{
    if (r->headers_out.status != NGX_HTTP_OK
        || r != r->main
        || r->disable_not_modified)
    {
        return ngx_http_next_header_filter(r);
    }

    if (r->headers_in.if_unmodified_since
        && !ngx_http_test_if_unmodified(r))
    {
        return ngx_http_filter_finalize_request(r, NULL,
                                                NGX_HTTP_PRECONDITION_FAILED);
    }

    if (r->headers_in.if_match
        && !ngx_http_test_if_match(r, r->headers_in.if_match, 0))
    {
        return ngx_http_filter_finalize_request(r, NULL,
                                                NGX_HTTP_PRECONDITION_FAILED);
    }

But, lua-nginx-module still allowed to output using "ngx.say()", as follows:

static int
ngx_http_lua_ngx_echo(lua_State *L, unsigned newline)
{
    ngx_http_request_t          *r;
    ngx_http_lua_ctx_t          *ctx;
    const char                  *p;
    size_t                       len;
    size_t                       size;
    ngx_buf_t                   *b;
    ngx_chain_t                 *cl;
    ngx_int_t                    rc;
    int                          i;
    int                          nargs;
    int                          type;
    const char                  *msg;

    r = ngx_http_lua_get_req(L);
    if (r == NULL) {
        return luaL_error(L, "no request object found");
    }

    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);

    if (ctx == NULL) {
        return luaL_error(L, "no request ctx found");
    }

    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);

Will u conside this runtime error as a bug?

Thks

spacewander commented 6 years ago

Looks like https://github.com/openresty/lua-nginx-module/issues/1131

tokers commented 6 years ago

Yes, this is caused by the filter_finalize logic in Nginx.