Permalink
Browse files

bugfix: upstream data buffers were not marked as fully consumed when …

…body_filter_by_lua* is used and ngx.arg[1] is overritten. this could result in connection hang for large response bodies. thanks Tzury Bar Yochay for reporting this issue.
  • Loading branch information...
1 parent a96dd55 commit 5a539500c13f6303fc25d77514571aab0c84350c @agentzh agentzh committed Jul 1, 2012
Showing with 58 additions and 4 deletions.
  1. +13 −3 src/ngx_http_lua_bodyfilterby.c
  2. +44 −0 t/082-body-filter.t
  3. +1 −1 util/build2.sh
@@ -537,8 +537,18 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
case LUA_TNIL:
/* discard the buffers */
- lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key);
- lua_pushlightuserdata(L, NULL);
+ lua_pushlightuserdata(L, &ngx_http_lua_body_filter_chain_key); /* key */
+ lua_pushvalue(L, -1); /* key key */
+ lua_rawget(L, LUA_GLOBALSINDEX); /* key val */
+ in = lua_touserdata(L, -1);
+ lua_pop(L, 1); /* key */
+
+ for (cl = in; cl; cl = cl->next) {
+ dd("mark the buf as consumed: %d", (int) ngx_buf_size(cl->buf));
+ cl->buf->pos = cl->buf->last;
+ }
+
+ lua_pushlightuserdata(L, NULL); /* key val */
lua_rawset(L, LUA_GLOBALSINDEX);
return 0;
@@ -564,7 +574,7 @@ ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
last = 1;
}
- /* mark the buf as consumed */
+ dd("mark the buf as consumed: %d", (int) ngx_buf_size(cl->buf));
cl->buf->pos = cl->buf->last;
}
View
@@ -410,3 +410,47 @@ hello!
--- no_error_log
[error]
+
+
+=== TEST 16: fully buffered output (string scalar, buffering to disk by ngx_proxy)
+--- config
+ location /t {
+ proxy_pass http://127.0.0.1:$server_port/stub;
+ proxy_buffers 2 256;
+ proxy_busy_buffers_size 256;
+ proxy_buffer_size 256;
+
+ body_filter_by_lua '
+ local chunk, eof = ngx.arg[1], ngx.arg[2]
+ local buf = ngx.ctx.buf
+
+ if eof then
+ if buf then
+ ngx.arg[1] = "[" .. buf .. chunk .. "]"
+ return
+ end
+
+ return
+ end
+
+ if buf then
+ ngx.ctx.buf = buf .. chunk
+ else
+ ngx.ctx.buf = chunk
+ end
+
+ ngx.arg[1] = nil
+ ';
+ }
+
+ location = /stub {
+ echo_duplicate 512 "a";
+ echo_duplicate 512 "b";
+ }
+--- request
+GET /t
+--- response_body eval
+"[" . ("a" x 512) . ("b" x 512) . "]";
+--- no_error_log
+[error]
+
View
@@ -3,7 +3,7 @@
# this file is mostly meant to be used by the author himself.
root=`pwd`
-version=${1:-1.0.5}
+version=${1:-1.2.1}
home=~
force=$2

0 comments on commit 5a53950

Please sign in to comment.