Permalink
Browse files

bugfix: the UDP cosocket object could no longer be used after an read…

… or write error happened.
  • Loading branch information...
1 parent d577a38 commit 0ee0c49cfd2d2c198816d61522c212b2540931eb @agentzh agentzh committed Aug 4, 2012
Showing with 65 additions and 15 deletions.
  1. +19 −14 src/ngx_http_lua_socket_udp.c
  2. +46 −1 t/087-udp-socket.t
@@ -683,18 +683,20 @@ ngx_http_lua_socket_udp_send(lua_State *L)
u = lua_touserdata(L, -1);
lua_pop(L, 1);
- if (u == NULL || u->udp_connection.connection == NULL || u->ft_type) {
+ if (u == NULL || u->udp_connection.connection == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "attempt to send data on a closed socket: u:%p, c:%p, "
- "ft:%ui",
- u, u ? u->udp_connection.connection : NULL,
- u ? u->ft_type : 0);
+ "attempt to send data on a closed socket: u:%p, c:%p",
+ u, u ? u->udp_connection.connection : NULL);
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}
+ if (u->ft_type) {
+ u->ft_type = 0;
+ }
+
if (u->waiting) {
lua_pushnil(L);
lua_pushliteral(L, "socket busy");
@@ -802,18 +804,20 @@ ngx_http_lua_socket_udp_receive(lua_State *L)
u = lua_touserdata(L, -1);
lua_pop(L, 1);
- if (u == NULL || u->udp_connection.connection == NULL || u->ft_type) {
+ if (u == NULL || u->udp_connection.connection == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
- "attempt to receive data on a closed socket: u:%p, c:%p, "
- "ft:%ui",
- u, u ? u->udp_connection.connection : NULL,
- u ? u->ft_type : 0);
+ "attempt to receive data on a closed socket: u:%p, c:%p",
+ u, u ? u->udp_connection.connection : NULL);
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
}
+ if (u->ft_type) {
+ u->ft_type = 0;
+ }
+
#if 1
if (u->waiting) {
lua_pushnil(L);
@@ -1021,11 +1025,13 @@ ngx_http_lua_socket_udp_read(ngx_http_request_t *r,
/* n == NGX_AGAIN */
+#if 1
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_lua_socket_udp_handle_error(r, u,
NGX_HTTP_LUA_SOCKET_FT_ERROR);
return NGX_ERROR;
}
+#endif
if (rev->active) {
ngx_add_timer(rev, u->read_timeout);
@@ -1050,6 +1056,8 @@ ngx_http_lua_socket_udp_read_handler(ngx_http_request_t *r,
"lua udp socket read handler");
if (c->read->timedout) {
+ c->read->timedout = 0;
+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"lua udp socket read timed out");
@@ -1299,10 +1307,7 @@ ngx_http_lua_socket_udp_close(lua_State *L)
u = lua_touserdata(L, -1);
lua_pop(L, 1);
- if (u == NULL
- || u->udp_connection.connection == NULL
- || u->ft_type)
- {
+ if (u == NULL || u->udp_connection.connection == NULL) {
lua_pushnil(L);
lua_pushliteral(L, "closed");
return 2;
View
@@ -3,7 +3,7 @@
use lib 'lib';
use Test::Nginx::Socket;
-repeat_each(10);
+repeat_each(2);
plan tests => repeat_each() * (3 * blocks() + 4);
@@ -492,3 +492,48 @@ GET /t
--- error_log
lua udp socket receive buffer size: 1400
+
+
+=== TEST 9: read timeout and resend
+--- config
+ location = /t {
+ content_by_lua '
+ local udp = ngx.socket.udp()
+ udp:settimeout(30)
+ local ok, err = udp:setpeername("127.0.0.1", 19232)
+ if not ok then
+ ngx.say("failed to setpeername: ", err)
+ return
+ end
+ local ok, err = udp:send("blah")
+ if not ok then
+ ngx.say("failed to send: ", err)
+ return
+ end
+ for i = 1, 2 do
+ local data, err = udp:receive()
+ if err == "timeout" then
+ -- continue
+ else
+ if not data then
+ ngx.say("failed to receive: ", err)
+ return
+ end
+ ngx.say("received: ", data)
+ return
+ end
+ end
+
+ ngx.say("timed out")
+ ';
+ }
+--- udp_listen: 19232
+--- udp_reply: hello world
+--- udp_reply_delay: 45ms
+--- request
+GET /t
+--- response_body
+received: hello world
+--- error_log
+lua udp socket read timed out
+

0 comments on commit 0ee0c49

Please sign in to comment.