Skip to content

Commit 75d6b61

Browse files
committed
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
1 parent 5a2650f commit 75d6b61

1 file changed

Lines changed: 11 additions & 20 deletions

File tree

nginx/ngx_http_js_module.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ static ngx_js_loc_conf_t *ngx_http_js_loc_conf(ngx_http_request_t *r);
365365
static ngx_js_ctx_t *ngx_http_js_ctx(ngx_http_request_t *r);
366366

367367
static void ngx_http_js_periodic_handler(ngx_event_t *ev);
368+
static void ngx_http_js_periodic_write_event_handler(ngx_http_request_t *r);
368369
static void ngx_http_js_periodic_shutdown_handler(ngx_event_t *ev);
369-
static void ngx_http_js_periodic_write_handler(ngx_event_t *ev);
370370
static void ngx_http_js_periodic_finalize(ngx_http_request_t *r, ngx_int_t rc);
371371
static void ngx_http_js_periodic_destroy(ngx_http_request_t *r,
372372
ngx_js_periodic_t *periodic);
@@ -3781,7 +3781,7 @@ ngx_http_js_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
37813781

37823782
ngx_js_del_event(ctx, event);
37833783

3784-
ngx_http_js_event_finalize(r->parent, rc);
3784+
ngx_http_js_event_finalize(r->parent, NGX_OK);
37853785

37863786
return NGX_OK;
37873787
}
@@ -4520,10 +4520,7 @@ ngx_http_js_periodic_handler(ngx_event_t *ev)
45204520
c->data = r;
45214521
c->destroyed = 0;
45224522
c->pool = r->pool;
4523-
c->read->log = &periodic->log;
45244523
c->read->handler = ngx_http_js_periodic_shutdown_handler;
4525-
c->write->log = &periodic->log;
4526-
c->write->handler = ngx_http_js_periodic_write_handler;
45274524

45284525
periodic->connection = c;
45294526
periodic->log_ctx.request = r;
@@ -4537,6 +4534,7 @@ ngx_http_js_periodic_handler(ngx_event_t *ev)
45374534
r->valid_unparsed_uri = 1;
45384535

45394536
r->health_check = 1;
4537+
r->write_event_handler = ngx_http_js_periodic_write_event_handler;
45404538

45414539
rc = ngx_http_js_init_vm(r, ngx_http_js_periodic_session_proto_id);
45424540

@@ -4574,17 +4572,12 @@ ngx_http_js_periodic_handler(ngx_event_t *ev)
45744572

45754573

45764574
static void
4577-
ngx_http_js_periodic_write_handler(ngx_event_t *ev)
4575+
ngx_http_js_periodic_write_event_handler(ngx_http_request_t *r)
45784576
{
4579-
ngx_connection_t *c;
4580-
ngx_http_js_ctx_t *ctx;
4581-
ngx_http_request_t *r;
4582-
4583-
c = ev->data;
4584-
r = c->data;
4577+
ngx_http_js_ctx_t *ctx;
45854578

4586-
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
4587-
"http js periodic write handler");
4579+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
4580+
"http js periodic write event handler");
45884581

45894582
ctx = ngx_http_get_module_ctx(r, ngx_http_js_module);
45904583

@@ -4656,10 +4649,6 @@ ngx_http_js_periodic_destroy(ngx_http_request_t *r, ngx_js_periodic_t *periodic)
46564649
c->fd = (ngx_socket_t) -1;
46574650
c->pool = NULL;
46584651
c->destroyed = 1;
4659-
4660-
if (c->write->posted) {
4661-
ngx_delete_posted_event(c->write);
4662-
}
46634652
}
46644653

46654654

@@ -4738,8 +4727,10 @@ ngx_http_js_event_finalize(ngx_http_request_t *r, ngx_int_t rc)
47384727
}
47394728

47404729
if (rc == NGX_OK) {
4741-
ngx_post_event(r->connection->write, &ngx_posted_events);
4730+
ngx_http_post_request(r, NULL);
47424731
}
4732+
4733+
ngx_http_run_posted_requests(r->connection);
47434734
}
47444735

47454736

@@ -5882,7 +5873,7 @@ ngx_http_qjs_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
58825873
JS_FreeValue(cx, reply);
58835874
ngx_js_del_event(ctx, event);
58845875

5885-
ngx_http_js_event_finalize(r->parent, rc);
5876+
ngx_http_js_event_finalize(r->parent, NGX_OK);
58865877

58875878
return NGX_OK;
58885879
}

0 commit comments

Comments
 (0)