* libs/core: Added garbage collector to luci.util.threadlocal to avoid memory leaks
[project/luci.git] / libs / core / luasrc / util.lua
index 3a0b2fb..5168560 100644 (file)
@@ -281,6 +281,13 @@ function threadlocal()
                        rawset(self, thread, {})
                end
                rawget(self, thread)[key] = value
+               
+               -- Avoid memory leaks by removing abandoned stores
+               for k, v in pairs(self) do
+                       if type(k) == "thread" and coroutine.status(k) == "dead" then
+                               rawset(self, k, nil)
+                       end
+               end
        end
        
        setmetatable(tbl, {__index = get, __newindex = set})