diff --git a/src/ngx_stream_lua_socket_tcp.c b/src/ngx_stream_lua_socket_tcp.c index 9bc38c31..57f389d0 100644 --- a/src/ngx_stream_lua_socket_tcp.c +++ b/src/ngx_stream_lua_socket_tcp.c @@ -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, @@ -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); diff --git a/t/058-tcp-socket.t b/t/058-tcp-socket.t index c4657a80..d0593fce 100644 --- a/t/058-tcp-socket.t +++ b/t/058-tcp-socket.t @@ -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; @@ -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) + + } + +--- error_log +shutdown on a not connected socket: closed