Skip to content

New Feature: added args support for ngx.on_abort #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5290,7 +5290,7 @@ Nginx API for Lua
This API was first enabled in the "v0.7.0" release.

ngx.on_abort
syntax: *ok, err = ngx.on_abort(callback)*
syntax: *ok, err = ngx.on_abort(callback, user_arg1, user_arg2, ...)*

context: *rewrite_by_lua*, access_by_lua*, content_by_lua**

Expand Down
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -4725,7 +4725,7 @@ This API was first enabled in the `v0.7.0` release.

ngx.on_abort
------------
**syntax:** *ok, err = ngx.on_abort(callback)*
**syntax:** *ok, err = ngx.on_abort(callback, user_arg1, user_arg2, ...)*

**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua**

Expand Down
2 changes: 1 addition & 1 deletion doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -4569,7 +4569,7 @@ And it will generate the following output:
This API was first enabled in the <code>v0.7.0</code> release.

== ngx.on_abort ==
'''syntax:''' ''ok, err = ngx.on_abort(callback)''
'''syntax:''' ''ok, err = ngx.on_abort(callback, user_arg1, user_arg2, ...)''

'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''

Expand Down
9 changes: 8 additions & 1 deletion src/ngx_http_lua_coroutine.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
lua_State *co; /* new coroutine to be created */
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_co_ctx_t *coctx; /* co ctx for the new coroutine */
int nargs;

luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected");
Expand Down Expand Up @@ -97,11 +98,17 @@ ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
lua_xmove(L, co, 1);
lua_replace(co, LUA_GLOBALSINDEX);

lua_xmove(mt, L, 1); /* move coroutine from main thread to L */
nargs = lua_gettop(L);


lua_pushvalue(L, 1); /* copy entry function to top of L*/
lua_xmove(L, co, 1); /* move entry function from L to co */

lua_xmove(L, co, nargs-1); /* L stack: func */
/* co stack: func [args] */

lua_xmove(mt, L, 1); /* move coroutine from main thread to L */

if (pcoctx) {
*pcoctx = coctx;
}
Expand Down
5 changes: 4 additions & 1 deletion src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3287,6 +3287,7 @@ ngx_http_lua_on_abort_resume(ngx_http_request_t *r)
ngx_connection_t *c;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_main_conf_t *lmcf;
int nargs;

ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
Expand All @@ -3306,7 +3307,9 @@ ngx_http_lua_on_abort_resume(ngx_http_request_t *r)

c = r->connection;

rc = ngx_http_lua_run_thread(lmcf->lua, r, ctx, 0);
nargs = lua_gettop(ctx->cur_co_ctx->co);

rc = ngx_http_lua_run_thread(lmcf->lua, r, ctx, nargs - 1);

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua run thread returned %d", rc);
Expand Down
43 changes: 42 additions & 1 deletion t/101-on-abort.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ our $StapScript = $t::StapThread::StapScript;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 4 + 16);
plan tests => repeat_each() * (blocks() * 4 + 17);

$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= '11211';
Expand Down Expand Up @@ -789,3 +789,44 @@ on abort called
[error]
[alert]

=== TEST 18: abort callback function with args
--- config
location /t {
lua_check_client_abort on;
content_by_lua '
function ab(a, b, c)
ngx.log(ngx.NOTICE, a .. b .. c .. " on abort called")
end
local ok, err = ngx.on_abort(ab, "foo", "bar", 3)
if not ok then
error("cannot set on_abort: " .. err)
end

ngx.sleep(0.7)
ngx.log(ngx.NOTICE, "main handler done")
';
}
--- request
GET /t

--- stap2 eval: $::StapScript
--- stap eval: $::GCScript
--- stap_out
create 2 in 1
lua check broken conn
terminate 2: ok
terminate 1: ok
delete thread 2
delete thread 1
lua req cleanup

--- timeout: 0.2
--- abort
--- wait: 0.7
--- ignore_response
--- no_error_log
[error]
--- error_log
client prematurely closed connection
foobar3 on abort called
main handler done