degesch: Lua: avoid resource leak

If a connector's on_success callback fails, we need to destroy the connection.
This commit is contained in:
Přemysl Eric Janouch 2016-01-07 01:30:19 +01:00
parent 376bbea249
commit 6754c59890
1 changed files with 16 additions and 10 deletions

View File

@ -8091,12 +8091,9 @@ struct lua_connection
int socket_fd; ///< Underlying connected socket
};
static int
lua_connection_close (lua_State *L)
static void
lua_connection_discard (struct lua_connection *self)
{
struct lua_connection *self =
luaL_checkudata (L, 1, XLUA_CONNECTION_METATABLE);
if (self->socket_fd != -1)
{
poller_fd_reset (&self->socket_event);
@ -8105,7 +8102,13 @@ lua_connection_close (lua_State *L)
}
// Connection is dead, we don't need to hold onto any resources anymore
lua_cache_invalidate (L, self);
lua_cache_invalidate (self->plugin->L, self);
}
static int
lua_connection_close (lua_State *L)
{
lua_connection_discard (luaL_checkudata (L, 1, XLUA_CONNECTION_METATABLE));
return 0;
}
@ -8187,9 +8190,7 @@ lua_connector_discard (struct lua_connector *self)
static int
lua_connector_abort (lua_State *L)
{
struct lua_connector *self =
luaL_checkudata (L, 1, XLUA_CONNECTOR_METATABLE);
lua_connector_discard (self);
lua_connector_discard (luaL_checkudata (L, 1, XLUA_CONNECTOR_METATABLE));
return 0;
}
@ -8214,11 +8215,16 @@ lua_connector_on_connected (void *user_data, int socket, const char *hostname)
{
lua_State *L = self->plugin->L;
lua_rawgeti (L, LUA_REGISTRYINDEX, self->ref_on_success);
lua_plugin_push_connection (self->plugin, socket); // 1: connection
struct lua_connection *connection =
lua_plugin_push_connection (self->plugin, socket); // 1: connection
struct error *e = NULL;
if (!lua_plugin_call (self->plugin, 1, 0, &e))
{
lua_plugin_log_error (self->plugin, "connector on_success", e);
// The connection has placed itself in the cache
lua_connection_discard (connection);
}
}
lua_connector_discard (self);