openresty / openresty/lua-nginx-module

runtime error: no request ctx found

Open
#1,350 2 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

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

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

Reproduce the supplied nginx configuration and curl request, then trace ngx_http_not_modified_header_filter_module.c through ngx_http_lua_ngx_echo and its request-context checks. Compare the finalized request state with the contexts allowed for ngx.say. Done means the bug status and expected behavior after request finalization are established, with a verified fix if the behavior is incorrect.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.