Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: correctly error out when ngx.socket.tcp is shutdown before connect #331

Merged
merged 1 commit into from
Dec 25, 2023
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
38 changes: 19 additions & 19 deletions src/ngx_stream_lua_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3185,6 +3185,25 @@ ngx_stream_lua_socket_tcp_shutdown(lua_State *L)
return luaL_error(L, "no request found");
}

if (u == NULL
|| u->peer.connection == NULL
|| (u->read_closed && u->write_closed))
{
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}

if (u->write_closed) {
lua_pushnil(L);
lua_pushliteral(L, "already shutdown");
return 2;
}

if (u->request != r) {
return luaL_error(L, "bad request");
}

ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module);
if (ctx == NULL) {
ngx_stream_lua_socket_handle_write_error(r, u,
Expand Down Expand Up @@ -3212,25 +3231,6 @@ ngx_stream_lua_socket_tcp_shutdown(lua_State *L)
ctx->eof = 1;
}

if (u == NULL
|| u->peer.connection == NULL
|| (u->read_closed && u->write_closed))
{
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}

if (u->write_closed) {
lua_pushnil(L);
lua_pushliteral(L, "already shutdown");
return 2;
}

if (u->request != r) {
return luaL_error(L, "bad request");
}

ngx_stream_lua_socket_check_busy_connecting(r, u, L);
ngx_stream_lua_socket_check_busy_writing(r, u, L);

Expand Down
21 changes: 20 additions & 1 deletion t/058-tcp-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;

repeat_each(2);

plan tests => repeat_each() * 219;
plan tests => repeat_each() * 221;

our $HtmlDir = html_dir;

Expand Down Expand Up @@ -3526,3 +3526,22 @@ orld
[error]
--- error_log
lua tcp socket calling receiveany() method to read at most 7 bytes



=== TEST 67: shutdown on a not connected socket correctly throws error
--- stream_server_config
lua_socket_connect_timeout 1s;
resolver $TEST_NGINX_RESOLVER ipv6=off;
resolver_timeout 3s;

content_by_lua_block {
local sock = ngx.socket.tcp()

local ok, err = sock:shutdown('send')
ngx.log(ngx.ERR, 'shutdown on a not connected socket: ', err)

fffonion marked this conversation as resolved.
Show resolved Hide resolved
}

--- error_log
shutdown on a not connected socket: closed
Loading