# HG changeset patch # User jbe # Date 1428596635 -7200 # Node ID a14e0eb8598b5d6c9e1732d40d245588c90cedd6 # Parent 110493d29f909c04437694dafd4e6e7e6bac2d8c Catching state-related errors properly in moonbridge_io.poll(...) diff -r 110493d29f90 -r a14e0eb8598b moonbridge_io.c --- a/moonbridge_io.c Thu Apr 09 16:38:16 2015 +0200 +++ b/moonbridge_io.c Thu Apr 09 18:23:55 2015 +0200 @@ -861,8 +861,12 @@ if (lua_toboolean(L, -1)) { handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY); if (handle) { + if (handle->closed) luaL_error(L, "Attempt to poll a closed connection"); fd = handle->fd; - if (fd < 0) luaL_error(L, "Handle in illegal state"); /* TODO: EOF simulation with finishing local sockets */ + if (fd < 0) { /* fake EOF to simulate shutdown */ + lua_pushboolean(L, 1); + return 1; + } } else { listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY); if (listener) { @@ -884,17 +888,14 @@ if (lua_toboolean(L, -1)) { handle = luaL_testudata(L, -2, MOONBR_IO_HANDLE_MT_REGKEY); if (handle) { + if (handle->closed) luaL_error(L, "Attempt to poll a closed connection"); + if (handle->finished) luaL_error(L, "Attempt to write-poll a finished connection"); fd = handle->fd; - if (fd < 0) luaL_error(L, "Handle in illegal state"); } else { listener = luaL_testudata(L, -2, MOONBR_IO_LISTENER_MT_REGKEY); - if (listener) { - fd = listener->fd; - if (fd < 0) luaL_error(L, "Attempt to poll a closed listener"); - } else { - fd = lua_tointegerx(L, -2, &isnum); - if (!isnum) luaL_error(L, "Expected integer (file descriptor), I/O handle, or listener in table key"); - } + if (listener) luaL_error(L, "Attempt to write-poll a listener"); + fd = lua_tointegerx(L, -2, &isnum); + if (!isnum) luaL_error(L, "Expected integer (file descriptor) or I/O handle in table key"); } FD_SET(fd, &writefds); if (fd+1 > nfds) nfds = fd+1;