]> git.kaiwu.me - njs.git/commit
HTTP: improved r.subrequest() error handling after d34fcb0 (0.8.5).
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 6 Jan 2026 01:23:50 +0000 (17:23 -0800)
committerDmitry Volyntsev <xeioexception@gmail.com>
Wed, 7 Jan 2026 23:22:29 +0000 (15:22 -0800)
commit75d6b61a3800f77ba7ba75db445876d780ed1c18
tree9d7e4aa77fb26fa6a7437a832ed9b482a60b3c99
parent5a2650f31e5f67984c4066861a94cc6460712f21
HTTP: improved r.subrequest() error handling after d34fcb0 (0.8.5).

This reverts code changes introduced in d34fcb0. To fix the original
problem JS errors are not propagated as nginx errors in r.subrequest().

This also fixes a problem of a lost write event when the njs handler
making r.subrequest() is called from a lua handler as a subrequest:

nginx.conf:
    ...
    location = /subrequest {
        access_by_lua_file scripts/subrequest.lua;
proxy_pass  http://localhost:8080/pass;
    }

    location = /nested/subrequest {
        internal;
        js_content main.getToken;
    }

scripts/subrequest.lua:
    local res = ngx.location.capture('/nested/subrequest')
    ngx.log(ngx.STDERR, "Subrequest  status: " .. res.status)

    if res.status ~= 200 then
      ngx.log(ngx.STDERR, "Subrequest failed with status: " .. res.status)
      return nil
    end

    return res.body

main.js:
    async function getToken(r) {
        var reply = await r.subrequest('/api/auth');
        r.error("reply.status :: " + reply.status);

        var status = reply.status;
        r.return(status, "{"result":"OK!"}");
    }

    export default { getToken };

$ curl localhost:8080/subrequest

error.log (before the fix):
    ...
    *1 http js event finalize rc: 0
    *1 post event 0000B4BCA64C5FF0
    *1 http log handler
    *1 http request count:3 blk:0
    timer delta: 0
    posted event 0000B4BCA64C5FF0
    *1 delete posted event 0000B4BCA64C5FF0
    *1 http run request: "/subrequest?"
    *1 access phase: 11
    *1 lua access handler, uri:"/subrequest" c:2
    *1 lua run write event handler: timedout:0, ready:1, writing_raw_req_socket:0
    *1 useless lua write event handler <<<---

error.log (after the fix):
    ...
    *1 http js event finalize rc: 0
    *1 http posted request: "/nested/subrequest?"
    *1 http js content write event handler
    *1 http js content rc: 0
    *1 http finalize request: 0, "/nested/subrequest?" a:0, c:3
    *1 lua run post subrequest handler, rc:0 c:3
    *1 http log handler
    *1 http wake parent request: "/subrequest?"
    *1 http posted request: "/subrequest?"
    *1 access phase: 11
    *1 lua access handler, uri:"/subrequest" c:2
    *1 lua run subrequests done, resuming lua thread
nginx/ngx_http_js_module.c