summaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-01-07 01:30:19 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2016-01-07 22:49:53 +0100
commit6754c59890bb54f23494f0892e3576362fc14f8a (patch)
tree04d2e2da48aa4bbd16a05f5b8636cce560ee0d8d /degesch.c
parent376bbea249f17eda9c682ff7153f102990548888 (diff)
downloadxK-6754c59890bb54f23494f0892e3576362fc14f8a.tar.gz
xK-6754c59890bb54f23494f0892e3576362fc14f8a.tar.xz
xK-6754c59890bb54f23494f0892e3576362fc14f8a.zip
degesch: Lua: avoid resource leak
If a connector's on_success callback fails, we need to destroy the connection.
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/degesch.c b/degesch.c
index e1ec751..71f0e02 100644
--- a/degesch.c
+++ b/degesch.c
@@ -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);