Skip to content

Commit 6885462

Browse files
committed
feature: added new API function ngx.thread.kill() for killing a user "light thread". thanks aviramc for the original patch in #288.
1 parent 52b76a7 commit 6885462

5 files changed

+599
-4
lines changed

src/ngx_http_lua_socket_tcp.c

+3
Original file line numberDiff line numberDiff line change
@@ -4344,6 +4344,9 @@ ngx_http_lua_tcp_resolve_cleanup(void *data)
43444344
ngx_http_lua_socket_tcp_upstream_t *u;
43454345
ngx_http_lua_co_ctx_t *coctx = data;
43464346

4347+
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
4348+
"lua tcp socket abort resolver");
4349+
43474350
u = coctx->data;
43484351
if (u == NULL) {
43494352
return;

src/ngx_http_lua_uthread.c

+85-2
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@
2424

2525
static int ngx_http_lua_uthread_spawn(lua_State *L);
2626
static int ngx_http_lua_uthread_wait(lua_State *L);
27+
static int ngx_http_lua_uthread_kill(lua_State *L);
2728

2829

2930
void
3031
ngx_http_lua_inject_uthread_api(ngx_log_t *log, lua_State *L)
3132
{
3233
/* new thread table */
33-
lua_createtable(L, 0 /* narr */, 2 /* nrec */);
34+
lua_createtable(L, 0 /* narr */, 3 /* nrec */);
3435

3536
lua_pushcfunction(L, ngx_http_lua_uthread_spawn);
3637
lua_setfield(L, -2, "spawn");
3738

3839
lua_pushcfunction(L, ngx_http_lua_uthread_wait);
3940
lua_setfield(L, -2, "wait");
4041

42+
lua_pushcfunction(L, ngx_http_lua_uthread_kill);
43+
lua_setfield(L, -2, "kill");
44+
4145
lua_setfield(L, -2, "thread");
4246
}
4347

@@ -181,7 +185,7 @@ ngx_http_lua_uthread_wait(lua_State *L)
181185

182186
/* being the last one */
183187
lua_pushnil(L);
184-
lua_pushliteral(L, "already waited");
188+
lua_pushliteral(L, "already waited or killed");
185189
return 2;
186190

187191
default:
@@ -197,4 +201,83 @@ ngx_http_lua_uthread_wait(lua_State *L)
197201
return lua_yield(L, 0);
198202
}
199203

204+
205+
static int
206+
ngx_http_lua_uthread_kill(lua_State *L)
207+
{
208+
lua_State *sub_co;
209+
ngx_http_request_t *r;
210+
ngx_http_lua_ctx_t *ctx;
211+
ngx_http_lua_co_ctx_t *coctx, *sub_coctx;
212+
213+
r = ngx_http_lua_get_req(L);
214+
if (r == NULL) {
215+
return luaL_error(L, "no request found");
216+
}
217+
218+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
219+
if (ctx == NULL) {
220+
return luaL_error(L, "no request ctx found");
221+
}
222+
223+
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
224+
| NGX_HTTP_LUA_CONTEXT_ACCESS
225+
| NGX_HTTP_LUA_CONTEXT_CONTENT
226+
| NGX_HTTP_LUA_CONTEXT_TIMER);
227+
228+
coctx = ctx->cur_co_ctx;
229+
230+
sub_co = lua_tothread(L, 1);
231+
luaL_argcheck(L, sub_co, 1, "lua thread expected");
232+
233+
sub_coctx = ngx_http_lua_get_co_ctx(sub_co, ctx);
234+
235+
if (sub_coctx == NULL) {
236+
return luaL_error(L, "no co ctx found");
237+
}
238+
239+
if (!sub_coctx->is_uthread) {
240+
lua_pushnil(L);
241+
lua_pushliteral(L, "not user thread");
242+
return 2;
243+
}
244+
245+
if (sub_coctx->parent_co_ctx != coctx) {
246+
lua_pushnil(L);
247+
lua_pushliteral(L, "killer not parent");
248+
return 2;
249+
}
250+
251+
if (sub_coctx->pending_subreqs > 0) {
252+
lua_pushnil(L);
253+
lua_pushliteral(L, "pending subrequests");
254+
return 2;
255+
}
256+
257+
switch (sub_coctx->co_status) {
258+
case NGX_HTTP_LUA_CO_ZOMBIE:
259+
ngx_http_lua_del_thread(r, L, ctx, sub_coctx);
260+
ctx->uthreads--;
261+
262+
lua_pushnil(L);
263+
lua_pushliteral(L, "already terminated");
264+
return 2;
265+
266+
case NGX_HTTP_LUA_CO_DEAD:
267+
lua_pushnil(L);
268+
lua_pushliteral(L, "already waited or killed");
269+
return 2;
270+
271+
default:
272+
ngx_http_lua_cleanup_pending_operation(sub_coctx);
273+
ngx_http_lua_del_thread(r, L, ctx, sub_coctx);
274+
ctx->uthreads--;
275+
276+
lua_pushinteger(L, 1);
277+
return 1;
278+
}
279+
280+
/* not reacheable */
281+
}
282+
200283
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

t/062-count.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ probe process("$LIBLUA_PATH").function("rehashtab") {
414414
}
415415
--- stap_out2
416416
--- response_body
417-
thread: 2
417+
thread: 3
418418
--- no_error_log
419419
[error]
420420

t/098-uthread-wait.t

+1-1
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ delete thread 1
12741274
--- response_body
12751275
hello in thread
12761276
thread created: zombie
1277-
failed to run thread: already waited
1277+
failed to run thread: already waited or killed
12781278
--- no_error_log
12791279
[error]
12801280

0 commit comments

Comments
 (0)