Permalink
Browse files

bugfix: the lua light threads would leak in case of nginx filter fina…

…lization. this issue was caught by the "check leak" testing mode provided by Test::Nginx while running on Amazon EC2.
  • Loading branch information...
1 parent 2694d77 commit 5d0fb9feaeb653e70b0c2d97ddfea40c1db24c80 @agentzh agentzh committed Oct 10, 2012
Showing with 48 additions and 2 deletions.
  1. +25 −0 src/ngx_http_lua_util.c
  2. +12 −2 t/072-conditional-get.t
  3. +11 −0 t/StapThread.pm
@@ -882,6 +882,7 @@ ngx_http_lua_request_cleanup(void *data)
/* force coroutine handling the request quit */
if (ctx == NULL) {
+ dd("ctx is NULL");
return;
}
@@ -1332,6 +1333,10 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
/* being the entry thread aborted */
+ if (r->filter_finalize) {
+ ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
+ }
+
ngx_http_lua_request_cleanup(r);
dd("headers sent? %d", ctx->headers_sent ? 1 : 0);
@@ -1385,6 +1390,11 @@ ngx_http_lua_run_thread(lua_State *L, ngx_http_request_t *r,
lua_settop(L, 0);
ctx->cur_co_ctx->co_status = NGX_HTTP_LUA_CO_DEAD;
+
+ if (r->filter_finalize) {
+ ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
+ }
+
ngx_http_lua_request_cleanup(r);
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "lua handler aborted: "
@@ -1976,6 +1986,11 @@ ngx_http_lua_handle_exec(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_probe_coroutine_done(r, ctx->cur_co_ctx->co, 1);
ctx->cur_co_ctx->co_status = NGX_HTTP_LUA_CO_DEAD;
+
+ if (r->filter_finalize) {
+ ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
+ }
+
ngx_http_lua_request_cleanup(r);
if (ctx->exec_uri.data[0] == '@') {
@@ -2061,6 +2076,11 @@ ngx_http_lua_handle_exit(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_probe_coroutine_done(r, ctx->cur_co_ctx->co, 1);
ctx->cur_co_ctx->co_status = NGX_HTTP_LUA_CO_DEAD;
+
+ if (r->filter_finalize) {
+ ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
+ }
+
ngx_http_lua_request_cleanup(r);
if ((ctx->exit_code == NGX_OK
@@ -2318,6 +2338,11 @@ ngx_http_lua_handle_rewrite_jump(lua_State *L, ngx_http_request_t *r,
ngx_http_lua_probe_coroutine_done(r, ctx->cur_co_ctx->co, 1);
ctx->cur_co_ctx->co_status = NGX_HTTP_LUA_CO_DEAD;
+
+ if (r->filter_finalize) {
+ ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
+ }
+
ngx_http_lua_request_cleanup(r);
return NGX_OK;
@@ -1,16 +1,19 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib 'lib';
use Test::Nginx::Socket;
+use t::StapThread;
+
+our $GCScript = $t::StapThread::GCScript;
+our $StapScript = $t::StapThread::StapScript;
#worker_connections(1014);
#master_on();
#workers(2);
#log_level('warn');
repeat_each(2);
-#repeat_each(1);
-plan tests => repeat_each() * (blocks() * 3);
+plan tests => repeat_each() * (blocks() * 3 + 1);
#no_diff();
#no_long_string();
@@ -70,6 +73,13 @@ If-Modified-Since: Thu, 10 May 2012 07:50:59 GMT
GET /lua
--- more_headers
If-Unmodified-Since: Thu, 10 May 2012 07:50:47 GMT
+
+--- stap2 eval: $::StapScript
+--- stap eval: $::GCScript
+--- stap_out
+terminate 1: fail
+delete thread 1
+
--- response_body_like: 412 Precondition Failed
--- error_code: 412
--- error_log
View
@@ -203,12 +203,23 @@ F(ngx_http_lua_sleep_resume) {
M(http-lua-coroutine-done) {
t = gen_id($arg2)
printf("terminate coro %d: %s\n", t, $arg3 ? "ok" : "fail")
+ //print_ubacktrace()
}
+F(ngx_http_lua_ngx_echo) {
+ println("ngx.print or ngx.say")
+}
+
+F(ngx_http_lua_del_all_threads) {
+ println("del all threads")
+}
+
+/*
M(http-lua-info) {
msg = user_string($arg1)
printf("lua info: %s\n", msg)
}
+*/
F(ngx_http_lua_uthread_wait) {
t = gen_id($L)

0 comments on commit 5d0fb9f

Please sign in to comment.