Skip to content

Added a thread killing API #288

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3f39535
Added an optional parameter for ngx.req.set_header and ngx.req.clear_…
Apr 24, 2013
886e27b
Merged with 0.8.1
May 8, 2013
1c5e5b9
Changed the replace_underscores parameter to a table of parameters (o…
May 8, 2013
f2e849a
Added an options table to clean_header as well.
May 8, 2013
e1281a2
Merge branch 'master' of https://github.com/chaoslawful/lua-nginx-module
Sep 29, 2013
d4d2988
Removed unneeded variable.
Sep 29, 2013
2a84a4c
Added two killing functions -
Sep 30, 2013
7cd287a
del_thread doesn't remove the sleeping timer, which may cause segfaul…
Sep 30, 2013
e699fcc
Merge branch 'master' of https://github.com/chaoslawful/lua-nginx-module
Oct 3, 2013
0a6a890
Corrected the killing of a thread due to a useful review by @agentzh
Oct 3, 2013
e9e1dec
Reverted unnecessary changes.
Oct 3, 2013
24cb685
Reverted unnecessary changes.
Oct 3, 2013
571d777
Reverted unnecessary changes.
Oct 3, 2013
974112f
Merge remote-tracking branch 'original-repository/master'
Dec 10, 2013
5d9ac66
Merge branch 'master' into thread-kill
Dec 10, 2013
5d52ad2
ngx_http_lua_del_thread now returns rc
Dec 11, 2013
16616b3
Some fixes according to remarks by @agentz :
Dec 11, 2013
48fdf3d
Merge branch 'master' of https://github.com/chaoslawful/lua-nginx-module
Dec 18, 2013
a2f7b55
Merge branch 'master' into thread-kill
Dec 18, 2013
305f0a5
Merge branch 'master' of https://github.com/chaoslawful/lua-nginx-module
Dec 24, 2013
ec7b791
Merge branch 'master' into thread-kill
Dec 24, 2013
8d62a09
Added another way to verify that a thread is dead.
Dec 24, 2013
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
85 changes: 84 additions & 1 deletion src/ngx_http_lua_uthread.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

static int ngx_http_lua_uthread_spawn(lua_State *L);
static int ngx_http_lua_uthread_wait(lua_State *L);

static int ngx_http_lua_uthread_kill(lua_State *L);

void
ngx_http_lua_inject_uthread_api(ngx_log_t *log, lua_State *L)
Expand All @@ -38,6 +38,9 @@ ngx_http_lua_inject_uthread_api(ngx_log_t *log, lua_State *L)
lua_pushcfunction(L, ngx_http_lua_uthread_wait);
lua_setfield(L, -2, "wait");

lua_pushcfunction(L, ngx_http_lua_uthread_kill);
lua_setfield(L, -2, "kill");

lua_setfield(L, -2, "thread");
}

Expand Down Expand Up @@ -196,4 +199,84 @@ ngx_http_lua_uthread_wait(lua_State *L)
return lua_yield(L, 0);
}


static int
ngx_http_lua_uthread_kill(lua_State *L)
{
int i, nargs;
int killed;
lua_State *sub_co;
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_co_ctx_t *coctx, *sub_coctx;

r = ngx_http_lua_get_req(L);
if (r == NULL) {
return luaL_error(L, "no request 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
| NGX_HTTP_LUA_CONTEXT_TIMER);

coctx = ctx->cur_co_ctx;

nargs = lua_gettop(L);

killed = 0;
for (i = 1; i <= nargs; i++) {
sub_co = lua_tothread(L, i);

luaL_argcheck(L, sub_co, i, "lua thread expected");

sub_coctx = ngx_http_lua_get_co_ctx(sub_co, ctx);
if (sub_coctx == NULL
|| sub_coctx->co_status == NGX_HTTP_LUA_CO_DEAD)
{
/* XXX The thread is dead, consider it a success and continue. */
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"attempt to kill an already dead thread");
killed++;
continue;
}

if (!sub_coctx->is_uthread) {
return luaL_error(L, "attempt to kill a coroutine that is "
"not a user thread");
}

if (sub_coctx->parent_co_ctx != coctx) {
return luaL_error(L, "only the parent coroutine can kill the "
"thread");
}

if (sub_coctx->nsubreqs) {
/* XXX Don't kill threads with pending subrequests. */
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"attempt to kill thread with pending subrequests");
continue;
}

if (sub_coctx->cleanup) {
sub_coctx->cleanup(sub_coctx);
sub_coctx->cleanup = NULL;
}

if (ngx_http_lua_del_thread(r, L, ctx, sub_coctx) == NGX_OK) {
killed++;
ctx->uthreads--;
}

}

lua_pushinteger(L, killed);
return 1;
}

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
6 changes: 4 additions & 2 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,12 @@ ngx_http_lua_new_thread(ngx_http_request_t *r, lua_State *L, int *ref)
}


void
ngx_int_t
ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t *coctx)
{
if (coctx->co_ref == LUA_NOREF) {
return;
return NGX_DECLINED;
}

ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Expand All @@ -365,6 +365,8 @@ ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
coctx->co_status = NGX_HTTP_LUA_CO_DEAD;

lua_pop(L, 1);

return NGX_OK;
}


Expand Down
2 changes: 1 addition & 1 deletion src/ngx_http_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ ngx_int_t ngx_http_lua_run_posted_threads(ngx_connection_t *c, lua_State *L,
ngx_int_t ngx_http_lua_post_thread(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t *coctx);

void ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
ngx_int_t ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_co_ctx_t *coctx);

void ngx_http_lua_rd_check_broken_connection(ngx_http_request_t *r);
Expand Down