* libs/httpd: Fixed garbage collection
[project/luci.git] / libs / httpd / luasrc / httpd.lua
index 8b54f0e645810671793c6e550a33d70d5ea4efa3..541063cc7e21cf16c6132076d75e76fb868b3615 100644 (file)
@@ -30,6 +30,11 @@ local threads = {}
 local threadm = {}
 local threadi = {}
 
+local _meta = {__mode = "k"}
+setmetatable(threadm, _meta)
+setmetatable(threadi, _meta)
+
+
 function Socket(ip, port)
        local sock, err = socket.bind( ip, port )
 
@@ -87,7 +92,6 @@ end
 
 function step()
        local idle = true
-               
        if not THREAD_LIMIT or threadc < THREAD_LIMIT then
                local now = os.time()
                for i, server in ipairs(reading) do
@@ -104,8 +108,8 @@ function step()
                coroutine.resume(thread, client)
                local now = os.time()
                if coroutine.status(thread) == "dead" then
-                       threads[client] = nil
                        threadc = threadc - 1
+                       threads[client] = nil
                elseif threadm[client] and threadm[client] + THREAD_TIMEOUT < now then
                        threads[client] = nil
                        threadc = threadc - 1   
@@ -117,6 +121,7 @@ function step()
        end
        
        if idle then
+               collectgarbage()
                socket.sleep(THREAD_IDLEWAIT)
        end
-end
\ No newline at end of file
+end