Skip to content

Commit a6a0ed5

Browse files
committed
bugfix: memory leak cound happen when both cosocket connection pools and "lua_code_cache off" were used. this regression had appeared in the ssl cosocket patch, i.e., commit 5aed196.
1 parent 531fb5a commit a6a0ed5

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

src/ngx_http_lua_socket_tcp.c

+38
Original file line numberDiff line numberDiff line change
@@ -3269,6 +3269,7 @@ ngx_http_lua_socket_tcp_close_connection(ngx_connection_t *c)
32693269

32703270
if (c->pool) {
32713271
ngx_destroy_pool(c->pool);
3272+
c->pool = NULL;
32723273
}
32733274

32743275
ngx_close_connection(c);
@@ -5158,6 +5159,43 @@ ngx_http_lua_ssl_free_session(lua_State *L)
51585159
return 0;
51595160
}
51605161

5162+
5163+
void
5164+
ngx_http_lua_cleanup_conn_pools(lua_State *L)
5165+
{
5166+
ngx_queue_t *q;
5167+
ngx_connection_t *c;
5168+
ngx_http_lua_socket_pool_t *spool;
5169+
ngx_http_lua_socket_pool_item_t *item;
5170+
5171+
lua_pushlightuserdata(L, &ngx_http_lua_socket_pool_key);
5172+
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
5173+
5174+
lua_pushnil(L); /* first key */
5175+
while (lua_next(L, -2) != 0) {
5176+
/* tb key val */
5177+
spool = lua_touserdata(L, -1);
5178+
5179+
if (!ngx_queue_empty(&spool->cache)) {
5180+
q = ngx_queue_head(&spool->cache);
5181+
item = ngx_queue_data(q, ngx_http_lua_socket_pool_item_t, queue);
5182+
c = item->connection;
5183+
5184+
ngx_http_lua_socket_tcp_close_connection(c);
5185+
5186+
ngx_queue_remove(q);
5187+
5188+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
5189+
"lua tcp socket keepalive: free connection pool "
5190+
"for \"%s\"", spool->key);
5191+
}
5192+
5193+
lua_pop(L, 1);
5194+
}
5195+
5196+
lua_pop(L, 1);
5197+
}
5198+
51615199
#endif /* NGX_HTTP_SSL */
51625200

51635201
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_socket_tcp.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ typedef struct {
147147

148148
void ngx_http_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L);
149149
void ngx_http_lua_inject_req_socket_api(lua_State *L);
150+
void ngx_http_lua_cleanup_conn_pools(lua_State *L);
150151

151152

152153
#endif /* _NGX_HTTP_LUA_SOCKET_TCP_H_INCLUDED_ */

src/ngx_http_lua_util.c

+1-36
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "ngx_http_lua_timer.h"
4949
#include "ngx_http_lua_config.h"
5050
#include "ngx_http_lua_worker.h"
51+
#include "ngx_http_lua_socket_tcp.h"
5152

5253

5354
#if 1
@@ -118,7 +119,6 @@ static ngx_int_t
118119
ngx_http_lua_ctx_t *ctx);
119120
static lua_State * ngx_http_lua_new_state(lua_State *parent_vm,
120121
ngx_cycle_t *cycle, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log);
121-
static void ngx_http_lua_cleanup_conn_pools(lua_State *L);
122122
static int ngx_http_lua_get_raw_phase_context(lua_State *L);
123123

124124

@@ -3740,41 +3740,6 @@ ngx_http_lua_cleanup_vm(void *data)
37403740
}
37413741

37423742

3743-
static void
3744-
ngx_http_lua_cleanup_conn_pools(lua_State *L)
3745-
{
3746-
ngx_queue_t *q;
3747-
ngx_connection_t *c;
3748-
ngx_http_lua_socket_pool_t *spool;
3749-
ngx_http_lua_socket_pool_item_t *item;
3750-
3751-
lua_pushlightuserdata(L, &ngx_http_lua_socket_pool_key);
3752-
lua_rawget(L, LUA_REGISTRYINDEX); /* table */
3753-
3754-
lua_pushnil(L); /* first key */
3755-
while (lua_next(L, -2) != 0) {
3756-
/* tb key val */
3757-
spool = lua_touserdata(L, -1);
3758-
3759-
if (!ngx_queue_empty(&spool->cache)) {
3760-
q = ngx_queue_head(&spool->cache);
3761-
item = ngx_queue_data(q, ngx_http_lua_socket_pool_item_t, queue);
3762-
c = item->connection;
3763-
ngx_close_connection(c);
3764-
ngx_queue_remove(q);
3765-
3766-
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
3767-
"lua tcp socket keepalive: free connection pool "
3768-
"for \"%s\"", spool->key);
3769-
}
3770-
3771-
lua_pop(L, 1);
3772-
}
3773-
3774-
lua_pop(L, 1);
3775-
}
3776-
3777-
37783743
ngx_connection_t *
37793744
ngx_http_lua_create_fake_connection(void)
37803745
{

0 commit comments

Comments
 (0)