# HG changeset patch # User jbe # Date 1472895686 -7200 # Node ID f3988723732a10c67d57379876ccc43e34439300 # Parent 28bffa2af1ec0b2576f56a9f1596567c9415d029 Call tls_close always before closing file descriptor diff -r 28bffa2af1ec -r f3988723732a moonbridge_io.c --- a/moonbridge_io.c Fri Aug 26 23:15:56 2016 +0200 +++ b/moonbridge_io.c Sat Sep 03 11:41:26 2016 +0200 @@ -129,6 +129,9 @@ flags = fcntl(handle->fd, F_GETFL, 0); if (flags == -1) { moonbr_io_errmsg(); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif close(handle->fd); handle->fd = -1; handle->closed = 1; @@ -138,7 +141,10 @@ else flags &= ~O_NONBLOCK; if (fcntl(handle->fd, F_SETFL, flags) == -1) { moonbr_io_errmsg(); - close(handle->fd); + #ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif + close(handle->fd); handle->fd = -1; handle->closed = 1; luaL_error(L, "Unexpected error in fcntl call: %s", errmsg); @@ -155,6 +161,9 @@ } if (setsockopt(handle->fd, SOL_SOCKET, SO_LINGER, &lingerval, sizeof(lingerval))) { moonbr_io_errmsg(); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif close(handle->fd); handle->fd = -1; handle->closed = 1; @@ -174,6 +183,9 @@ if (setsockopt(handle->fd, IPPROTO_TCP, TCP_CORK, &nopush, sizeof(nopush))) { #endif moonbr_io_errmsg(); +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif close(handle->fd); handle->fd = -1; handle->closed = 1; @@ -752,6 +764,9 @@ return 2; } } else { +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif if (close(handle->fd)) { moonbr_io_errmsg(); handle->fd = -1; @@ -775,12 +790,18 @@ lua_pushvalue(L, 1); if (lua_pcall(L, 1, 2, 0)) { handle->closed = 1; +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif close(handle->fd); handle->fd = -1; lua_error(L); } handle->closed = 1; if (!lua_toboolean(L, -2)) { +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif close(handle->fd); handle->fd = -1; return 2; @@ -793,6 +814,9 @@ handle->closed = 1; } if (handle->fd >= 0) { +#ifdef MOONBR_IO_USE_TLS + if (handle->tls) tls_close(handle->tls); +#endif if (close(handle->fd)) { moonbr_io_errmsg(); handle->fd = -1; @@ -985,7 +1009,7 @@ lua_pushcfunction(L, moonbr_io_pushhandle_impl); lua_pushlightuserdata(L, &fd); if (lua_pcall(L, 1, 1, 0)) { - if (fd != -1) close(fd); + if (fd != -1) close(fd); // TODO: correct to close file descriptor here? lua_error(L); } }