Skip to content

Commit

Permalink
bugfix: memory leak cound happen when both cosocket connection pools …
Browse files Browse the repository at this point in the history
…and "lua_code_cache off" were used. this regression had appeared in the ssl cosocket patch, i.e., commit 5aed196.
  • Loading branch information
agentzh committed Jul 23, 2014
1 parent 531fb5a commit a6a0ed5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
38 changes: 38 additions & 0 deletions src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,7 @@ ngx_http_lua_socket_tcp_close_connection(ngx_connection_t *c)

if (c->pool) {
ngx_destroy_pool(c->pool);
c->pool = NULL;
}

ngx_close_connection(c);
Expand Down Expand Up @@ -5158,6 +5159,43 @@ ngx_http_lua_ssl_free_session(lua_State *L)
return 0;
}


void
ngx_http_lua_cleanup_conn_pools(lua_State *L)
{
ngx_queue_t *q;
ngx_connection_t *c;
ngx_http_lua_socket_pool_t *spool;
ngx_http_lua_socket_pool_item_t *item;

lua_pushlightuserdata(L, &ngx_http_lua_socket_pool_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */

lua_pushnil(L); /* first key */
while (lua_next(L, -2) != 0) {
/* tb key val */
spool = lua_touserdata(L, -1);

if (!ngx_queue_empty(&spool->cache)) {
q = ngx_queue_head(&spool->cache);
item = ngx_queue_data(q, ngx_http_lua_socket_pool_item_t, queue);
c = item->connection;

ngx_http_lua_socket_tcp_close_connection(c);

ngx_queue_remove(q);

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua tcp socket keepalive: free connection pool "
"for \"%s\"", spool->key);
}

lua_pop(L, 1);
}

lua_pop(L, 1);
}

#endif /* NGX_HTTP_SSL */

/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
1 change: 1 addition & 0 deletions src/ngx_http_lua_socket_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ typedef struct {

void ngx_http_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L);
void ngx_http_lua_inject_req_socket_api(lua_State *L);
void ngx_http_lua_cleanup_conn_pools(lua_State *L);


#endif /* _NGX_HTTP_LUA_SOCKET_TCP_H_INCLUDED_ */
Expand Down
37 changes: 1 addition & 36 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "ngx_http_lua_timer.h"
#include "ngx_http_lua_config.h"
#include "ngx_http_lua_worker.h"
#include "ngx_http_lua_socket_tcp.h"


#if 1
Expand Down Expand Up @@ -118,7 +119,6 @@ static ngx_int_t
ngx_http_lua_ctx_t *ctx);
static lua_State * ngx_http_lua_new_state(lua_State *parent_vm,
ngx_cycle_t *cycle, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
static void ngx_http_lua_cleanup_conn_pools(lua_State *L);
static int ngx_http_lua_get_raw_phase_context(lua_State *L);


Expand Down Expand Up @@ -3740,41 +3740,6 @@ ngx_http_lua_cleanup_vm(void *data)
}


static void
ngx_http_lua_cleanup_conn_pools(lua_State *L)
{
ngx_queue_t *q;
ngx_connection_t *c;
ngx_http_lua_socket_pool_t *spool;
ngx_http_lua_socket_pool_item_t *item;

lua_pushlightuserdata(L, &ngx_http_lua_socket_pool_key);
lua_rawget(L, LUA_REGISTRYINDEX); /* table */

lua_pushnil(L); /* first key */
while (lua_next(L, -2) != 0) {
/* tb key val */
spool = lua_touserdata(L, -1);

if (!ngx_queue_empty(&spool->cache)) {
q = ngx_queue_head(&spool->cache);
item = ngx_queue_data(q, ngx_http_lua_socket_pool_item_t, queue);
c = item->connection;
ngx_close_connection(c);
ngx_queue_remove(q);

ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"lua tcp socket keepalive: free connection pool "
"for \"%s\"", spool->key);
}

lua_pop(L, 1);
}

lua_pop(L, 1);
}


ngx_connection_t *
ngx_http_lua_create_fake_connection(void)
{
Expand Down

0 comments on commit a6a0ed5

Please sign in to comment.