Skip to content

Commit 2a19073

Browse files
committed
change: removed error logging on coroutine runtime errors.
1 parent 9a892b8 commit 2a19073

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

src/ngx_http_lua_util.c

+28-16
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,14 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
10201020

10211021
NGX_LUA_EXCEPTION_TRY {
10221022

1023+
/*
1024+
* silence a -Werror=clobbered warning with gcc 5.4
1025+
* due to above setjmp
1026+
*/
1027+
err = NULL;
1028+
msg = NULL;
1029+
trace = NULL;
1030+
10231031
if (ctx->cur_co_ctx->thread_spawn_yielded) {
10241032
ngx_http_lua_probe_info("thread spawn yielded");
10251033

@@ -1389,29 +1397,33 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
13891397
ctx->cur_co_ctx = orig_coctx;
13901398
}
13911399

1392-
if (lua_isstring(ctx->cur_co_ctx->co, -1)) {
1393-
dd("user custom error msg");
1394-
msg = lua_tostring(ctx->cur_co_ctx->co, -1);
1395-
1396-
} else {
1397-
msg = "unknown reason";
1398-
}
1399-
14001400
ngx_http_lua_cleanup_pending_operation(ctx->cur_co_ctx);
14011401

14021402
ngx_http_lua_probe_coroutine_done(r, ctx->cur_co_ctx->co, 0);
14031403

14041404
ctx->cur_co_ctx->co_status = NGX_HTTP_LUA_CO_DEAD;
14051405

1406-
ngx_http_lua_thread_traceback(L, ctx->cur_co_ctx->co,
1407-
ctx->cur_co_ctx);
1408-
trace = lua_tostring(L, -1);
1406+
if (orig_coctx->is_uthread
1407+
|| orig_coctx->is_wrap
1408+
|| ngx_http_lua_is_entry_thread(ctx))
1409+
{
1410+
ngx_http_lua_thread_traceback(L, orig_coctx->co, orig_coctx);
1411+
trace = lua_tostring(L, -1);
1412+
1413+
if (lua_isstring(orig_coctx->co, -1)) {
1414+
msg = lua_tostring(orig_coctx->co, -1);
1415+
dd("user custom error msg: %s", msg);
14091416

1410-
propagate_error:
1417+
} else {
1418+
msg = "unknown reason";
1419+
}
1420+
}
14111421

1412-
ngx_http_lua_assert(err != NULL && msg != NULL && trace != NULL);
1422+
propagate_error:
14131423

14141424
if (ctx->cur_co_ctx->is_uthread) {
1425+
ngx_http_lua_assert(err != NULL && msg != NULL
1426+
&& trace != NULL);
14151427

14161428
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
14171429
"lua user thread aborted: %s: %s\n%s",
@@ -1462,6 +1474,9 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
14621474
}
14631475

14641476
if (ngx_http_lua_is_entry_thread(ctx)) {
1477+
ngx_http_lua_assert(err != NULL && msg != NULL
1478+
&& trace != NULL);
1479+
14651480
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
14661481
"lua entry thread aborted: %s: %s\n%s",
14671482
err, msg, trace);
@@ -1519,9 +1534,6 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
15191534
lua_xmove(orig_coctx->co, next_co, 1);
15201535
nrets = 2;
15211536

1522-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1523-
"lua coroutine: %s: %s\n%s", err, msg, trace);
1524-
15251537
/* try resuming on the new coroutine again */
15261538
continue;
15271539
}

t/091-coroutine.t

+38-8
Original file line numberDiff line numberDiff line change
@@ -467,21 +467,25 @@ done
467467
468468
469469
=== TEST 10: thread traceback (multi-thread)
470+
Note: only coroutine.wrap propagates errors to the parent coroutine
471+
(and thus produces a traceback)
470472
--- config
471473
location /lua {
472-
content_by_lua '
474+
content_by_lua_block {
473475
local f = function(cr) coroutine.resume(cr) end
474476
-- emit a error
475477
local g = function() unknown.unknown = 1 end
476-
local l1 = coroutine.create(f)
477-
local l2 = coroutine.create(g)
478-
coroutine.resume(l1, l2)
478+
local l1 = coroutine.wrap(f)
479+
local l2 = coroutine.wrap(g)
480+
local l3 = coroutine.wrap(function() l1(l2) end)
481+
l3()
479482
ngx.say("hello")
480-
';
483+
}
481484
}
482485
--- request
483486
GET /lua
484-
--- response_body
487+
--- error_code: 500
488+
--- response_body_unlike
485489
hello
486490
--- error_log eval
487491
["stack traceback:", "coroutine 0:", "coroutine 1:", "coroutine 2:"]
@@ -904,8 +908,8 @@ qr/^child: resume: falsecontent_by_lua\(nginx\.conf:\d+\):4: bad
904908
child: status: dead
905909
parent: status: running
906910
$/s
907-
--- error_log eval
908-
qr/lua coroutine: runtime error: content_by_lua\(nginx\.conf:\d+\):4: bad/
911+
--- no_error_log
912+
[error]
909913
910914
911915
@@ -1712,3 +1716,29 @@ GET /t
17121716
--- grep_error_log eval: qr/init_by_lua error: .*? something went wrong/
17131717
--- grep_error_log_out
17141718
init_by_lua error: init_by_lua:7: init_by_lua:4: something went wrong
1719+
1720+
1721+
1722+
=== TEST 46: coroutine.resume runtime errors do not log errors
1723+
--- config
1724+
location = /t {
1725+
content_by_lua_block {
1726+
local function f()
1727+
error("something went wrong")
1728+
end
1729+
1730+
local ret1, ret2 = coroutine.resume(coroutine.create(f))
1731+
ngx.say(ret1)
1732+
ngx.say(ret2)
1733+
}
1734+
}
1735+
--- request
1736+
GET /t
1737+
--- response_body_like
1738+
false
1739+
content_by_lua\(nginx.conf:\d+\):\d+: something went wrong
1740+
--- no_error_log eval
1741+
[
1742+
qr/\[error\] .*? lua coroutine: runtime error:",
1743+
"stack traceback:",
1744+
]

0 commit comments

Comments
 (0)