Skip to content

Commit

Permalink
bugfix: correctly error out when ngx.socket.tcp is shutdown before
Browse files Browse the repository at this point in the history
connect
  • Loading branch information
fffonion committed Dec 1, 2023
1 parent 9135e7b commit ffb7442
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
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
22 changes: 22 additions & 0 deletions t/058-tcp-socket.t
Original file line number Diff line number Diff line change
Expand Up @@ -3526,3 +3526,25 @@ 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)

}

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



0 comments on commit ffb7442

Please sign in to comment.