BUG/MEDIUM: lua: forces a garbage collection

When a thread stops this patch forces a garbage collection which
cleanup and free the memory.

The HAProxy Lua Socket class contains an HAProxy session, so it
uses a big amount of memory, in other this Socket class uses a
filedescriptor and maintain a connection open.

If the class Socket is stored in a global variable, the Socket stay
alive along of the life of the process (except if it is closed by the
other size, or if a timeout is reached). If the class Socket is stored
in a local variable, it must die with this variable.

The socket is closed by a call from the garbage collector. And the
portability and use of a variable is known by the same garbage collector.

so, running the GC just after the end of all Lua code ensure that the
heavy resources like the socket class are freed quickly.
diff --git a/src/hlua.c b/src/hlua.c
index 30e294f..32b6f1a 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -911,9 +911,23 @@
 	/* Purge all the pending signals. */
 	hlua_com_purge(lua);
 
-	/* The thread is garbage collected by Lua. */
 	luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
 	luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref);
+
+	/* Forces a garbage collecting process. If the Lua program is finished
+	 * without error, we run the GC on the thread pointer. Its freed all
+	 * the unused memory.
+	 * If the thread is finnish with an error or is currently yielded,
+	 * it seems that the GC applied on the thread doesn't clean anything,
+	 * so e run the GC on the main thread.
+	 * NOTE: maybe this action locks all the Lua threads untiml the en of
+	 * the garbage collection.
+	 */
+	if (lua_status(lua->T) == LUA_OK)
+		lua_gc(lua->T, LUA_GCCOLLECT, 0);
+	else
+		lua_gc(gL.T, LUA_GCCOLLECT, 0);
+
 	lua->T = NULL;
 }