Skip to content

Commit ff7c65b

Browse files
authored
bugfix: fixed error message when socket is not connected.
connect
1 parent 9135e7b commit ff7c65b

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

src/ngx_stream_lua_socket_tcp.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,6 +3185,25 @@ ngx_stream_lua_socket_tcp_shutdown(lua_State *L)
31853185
return luaL_error(L, "no request found");
31863186
}
31873187

3188+
if (u == NULL
3189+
|| u->peer.connection == NULL
3190+
|| (u->read_closed && u->write_closed))
3191+
{
3192+
lua_pushnil(L);
3193+
lua_pushliteral(L, "closed");
3194+
return 2;
3195+
}
3196+
3197+
if (u->write_closed) {
3198+
lua_pushnil(L);
3199+
lua_pushliteral(L, "already shutdown");
3200+
return 2;
3201+
}
3202+
3203+
if (u->request != r) {
3204+
return luaL_error(L, "bad request");
3205+
}
3206+
31883207
ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module);
31893208
if (ctx == NULL) {
31903209
ngx_stream_lua_socket_handle_write_error(r, u,
@@ -3212,25 +3231,6 @@ ngx_stream_lua_socket_tcp_shutdown(lua_State *L)
32123231
ctx->eof = 1;
32133232
}
32143233

3215-
if (u == NULL
3216-
|| u->peer.connection == NULL
3217-
|| (u->read_closed && u->write_closed))
3218-
{
3219-
lua_pushnil(L);
3220-
lua_pushliteral(L, "closed");
3221-
return 2;
3222-
}
3223-
3224-
if (u->write_closed) {
3225-
lua_pushnil(L);
3226-
lua_pushliteral(L, "already shutdown");
3227-
return 2;
3228-
}
3229-
3230-
if (u->request != r) {
3231-
return luaL_error(L, "bad request");
3232-
}
3233-
32343234
ngx_stream_lua_socket_check_busy_connecting(r, u, L);
32353235
ngx_stream_lua_socket_check_busy_writing(r, u, L);
32363236

t/058-tcp-socket.t

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua::Stream;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 219;
7+
plan tests => repeat_each() * 221;
88

99
our $HtmlDir = html_dir;
1010

@@ -3526,3 +3526,22 @@ orld
35263526
[error]
35273527
--- error_log
35283528
lua tcp socket calling receiveany() method to read at most 7 bytes
3529+
3530+
3531+
3532+
=== TEST 67: shutdown on a not connected socket correctly throws error
3533+
--- stream_server_config
3534+
lua_socket_connect_timeout 1s;
3535+
resolver $TEST_NGINX_RESOLVER ipv6=off;
3536+
resolver_timeout 3s;
3537+
3538+
content_by_lua_block {
3539+
local sock = ngx.socket.tcp()
3540+
3541+
local ok, err = sock:shutdown('send')
3542+
ngx.log(ngx.ERR, 'shutdown on a not connected socket: ', err)
3543+
3544+
}
3545+
3546+
--- error_log
3547+
shutdown on a not connected socket: closed

0 commit comments

Comments
 (0)