From 88af2ea6e9728680b5f0216143246d8486952b91 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Sat, 4 Oct 2008 15:25:54 +0100 Subject: Session destruction fixes, some debugging code while we fix the rest. Also change logger to be more useful. --- core/sessionmanager.lua | 41 +++++++++++++++++++++++++++++++++++++++-- core/usermanager.lua | 2 ++ core/xmlhandlers.lua | 11 ++++++++++- main.lua | 10 ++++------ util/logger.lua | 2 +- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/core/sessionmanager.lua b/core/sessionmanager.lua index b257e532..3366120e 100644 --- a/core/sessionmanager.lua +++ b/core/sessionmanager.lua @@ -1,26 +1,62 @@ local tonumber, tostring = tonumber, tostring; -local ipairs, print= ipairs, print; - +local ipairs, pairs, print= ipairs, pairs, print; +local collectgarbage = collectgarbage; local m_random = import("math", "random"); local format = import("string", "format"); local hosts = hosts; +local sessions = sessions; local modulemanager = require "core.modulemanager"; local log = require "util.logger".init("sessionmanager"); local error = error; local uuid_generate = require "util.uuid".uuid_generate; + +local newproxy = newproxy; +local getmetatable = getmetatable; + module "sessionmanager" function new_session(conn) local session = { conn = conn, notopen = true, priority = 0, type = "c2s_unauthed" }; + if true then + session.trace = newproxy(true); + getmetatable(session.trace).__gc = function () print("Session got collected") end; + end local w = conn.write; session.send = function (t) w(tostring(t)); end return session; end function destroy_session(session) + if not (session and session.disconnect) then return; end + log("debug", "Destroying session..."); + session.disconnect(); + if session.username then + if session.resource then + hosts[session.host].sessions[session.username].sessions[session.resource] = nil; + end + local nomore = true; + for res, ssn in pairs(hosts[session.host].sessions[session.username]) do + nomore = false; + end + if nomore then + hosts[session.host].sessions[session.username] = nil; + end + end + session.conn = nil; + session.disconnect = nil; + for k in pairs(session) do + if k ~= "trace" then + session[k] = nil; + end + end + collectgarbage("collect"); + collectgarbage("collect"); + collectgarbage("collect"); + collectgarbage("collect"); + collectgarbage("collect"); end function send_to_session(session, data) @@ -34,6 +70,7 @@ function make_authenticated(session, username) if session.type == "c2s_unauthed" then session.type = "c2s"; end + return true; end function bind_resource(session, resource) diff --git a/core/usermanager.lua b/core/usermanager.lua index a67ad368..0f303b24 100644 --- a/core/usermanager.lua +++ b/core/usermanager.lua @@ -1,10 +1,12 @@ require "util.datamanager" local datamanager = datamanager; +local log = require "util.logger".init("usermanager"); module "usermanager" function validate_credentials(host, username, password) + log("debug", "User '%s' is being validated", username); local credentials = datamanager.load(username, host, "accounts") or {}; if password == credentials.password then return true; end return false; diff --git a/core/xmlhandlers.lua b/core/xmlhandlers.lua index 1ce7527d..ebc8f91d 100644 --- a/core/xmlhandlers.lua +++ b/core/xmlhandlers.lua @@ -10,6 +10,7 @@ local t_insert = table.insert; local t_remove = table.remove; local t_concat = table.concat; local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end +local sm_destroy_session = import("core.sessionmanager", "destroy_session"); local error = error; @@ -60,7 +61,15 @@ function init_xmlhandlers(session) end function xml_handlers:EndElement(name) curr_ns,name = name:match("^(.+):(%w+)$"); - if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then error("XML parse error in client stream"); end + if (not stanza) or #stanza.last_add < 0 or (#stanza.last_add > 0 and name ~= stanza.last_add[#stanza.last_add].name) then + if name == "stream" then + log("debug", "Stream closed"); + sm_destroy_session(session); + return; + else + error("XML parse error in client stream"); + end + end if stanza and #chardata > 0 then -- We have some character data in the buffer stanza:text(t_concat(chardata)); diff --git a/main.lua b/main.lua index 027a472c..821b3273 100644 --- a/main.lua +++ b/main.lua @@ -33,6 +33,7 @@ local t_concat = table.concat; local t_concatall = function (t, sep) local tt = {}; for _, s in ipairs(t) do t_insert(tt, tostring(s)); end return t_concat(tt, sep); end local m_random = math.random; local format = string.format; +local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session; --import("core.sessionmanager", "new_session", "destroy_session"); local st = stanza; ------------------------------ @@ -48,7 +49,7 @@ function handler(conn, data, err) local session = sessions[conn]; if not session then - sessions[conn] = sessionmanager.new_session(conn); + sessions[conn] = sm_new_session(conn); session = sessions[conn]; -- Logging functions -- @@ -75,11 +76,8 @@ function handler(conn, data, err) pres:tag("status"):text("Disconnected: "..err); session.stanza_dispatch(pres); end - if session.username then - hosts[session.host].sessions[session.username] = nil; - end session = nil; - print("Disconnected: "..err); + print("Disconnected: "..tostring(err)); collectgarbage("collect"); end end @@ -91,7 +89,7 @@ function handler(conn, data, err) end function disconnect(conn, err) - sessions[conn].disconnect(err); + sm_destroy_session(sessions[conn]); sessions[conn] = nil; end diff --git a/util/logger.lua b/util/logger.lua index 3d672e94..623ceb67 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -9,7 +9,7 @@ function init(name) name = nil; -- While this line is not commented, will automatically fill in file/line number info return function (level, message, ...) if not name then - local inf = debug.getinfo(2, 'Snl'); + local inf = debug.getinfo(3, 'Snl'); level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; end if ... then -- cgit v1.2.3