Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ngx_http_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4211,10 +4211,15 @@ ngx_http_lua_socket_tcp_finalize_read_part(ngx_http_request_t *r,
ngx_memzero(&u->buffer, sizeof(ngx_buf_t));
}

/* mirror tcp_finalize: detach cp so its __gc is safe */
/*
* mirror tcp_finalize: detach cp so its __gc is safe, and drop our
* reference to cp as well, since cp may be freed by the Lua GC before
* the socket is finalized.
*/
if (u->input_filter_ctx != NULL && u->input_filter_ctx != u) {
((ngx_http_lua_socket_compiled_pattern_t *)
u->input_filter_ctx)->upstream = NULL;
u->input_filter_ctx = NULL;
}

if (u->raw_downstream || u->body_downstream) {
Expand Down
71 changes: 71 additions & 0 deletions t/066-socket-receiveuntil.t
Original file line number Diff line number Diff line change
Expand Up @@ -2012,3 +2012,74 @@ close: 1 nil
--- no_error_log
[error]
--- skip_eval: 3:$ENV{TEST_NGINX_USE_HTTP3}



=== TEST 27: close the socket after the iterator got a read error and was GC'ed (GH #2517)
--- config
server_tokens off;
location /t {
content_by_lua_block {
-- JIT traces may keep the iterator closure alive
jit.off()
jit.flush()

local sock = ngx.socket.tcp()
local port = ngx.var.server_port

local ok, err = sock:connect("127.0.0.1", port)
if not ok then
ngx.say("failed to connect: ", err)
return
end

local req = "GET /foo HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n"

local bytes, err = sock:send(req)
if not bytes then
ngx.say("failed to send request: ", err)
return
end

local weak = setmetatable({}, { __mode = "v" })

local function read_until_error()
local reader = sock:receiveuntil("--no-such-boundary")
weak[1] = reader

while true do
local data, err = reader(1)
if not data then
ngx.say("failed to read: ", err)
return
end
end
end

read_until_error()

for _ = 1, 4 do
collectgarbage("collect")
end

ngx.say("reader collected: ", weak[1] == nil)

ok, err = sock:close()
ngx.say("close: ", ok, " ", err)
}
}

location /foo {
content_by_lua_block {
ngx.print("hello world")
}
}
--- request
GET /t
--- response_body
failed to read: closed
reader collected: true
close: 1 nil
--- no_error_log
[error]
--- skip_eval: 3:$ENV{TEST_NGINX_USE_HTTP3}
Loading