From 6754c59890bb54f23494f0892e3576362fc14f8a Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Thu, 7 Jan 2016 01:30:19 +0100
Subject: degesch: Lua: avoid resource leak
If a connector's on_success callback fails, we need to destroy the connection.
---
degesch.c | 26 ++++++++++++++++----------
1 file 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);
--
cgit v1.2.3-70-g09d2