lunarmodules / luasql

LuaSQL is a simple interface from Lua to a DBMS.
http://lunarmodules.github.io/luasql
535 stars 192 forks source link

Out of memory with MariaDB #109

Open krejkrejkrej opened 5 years ago

krejkrejkrej commented 5 years ago

Hi. I'm having a problem with out-of-memory errors when connecting repeatedly to a MariaDB. This is within Domoticz using: Lua: 5.2.2 lua-sql-mysql - Version: 2.3.4-1 libmariadbclient18 - Version: 10.1.37-0+deb9u1 Debian Linux 9 in a Docker container.

I guess I'm seeing the same problem as in issue #105 , so let me add some more detail. There's a Lua script that is executed once a minute. After restarting Domoticz, the script happily fetches data for about 16 hours, after that I see:

2019-01-22 19:13:00.436 Error: EventSystem: in Hämta VP-data: [string "--..."]:40: LuaSQL: error connecting: Out of memory.

Line 40 is "env:connect()" This is what the script does using Luasql:

-- connect to MariaDB
package.cpath = package.cpath .. ";/usr/lib/x86_64-linux-gnu/lua/5.2/luasql/?.so"
local mysql_driver = require "luasql.mysql"
local env  = assert(mysql_driver.mysql())
local conn = assert(env:connect('thermiq_db', 'thermiq_user', 'REMOVED', 'db.ejvind.se'))

-- execute the query
local cursor = assert(conn:execute([[
SELECT
   T_UTE,
   T_VATTEN, 
   (CASE WHEN kompr = 1 THEN 2100 ELSE 0 END + TS_P * 1000)  AS EFFEKT,
   (CASE WHEN ts_p = 0 THEN 0 
         WHEN ts_p > 0 AND varmvatten = 1 THEN 1 
         ELSE 4
     END)                                                    AS ELPATRON,
    (CASE WHEN kompr = 0 THEN 0
          WHEN kompr = 1 AND varmvatten = 0 THEN 1
          ELSE 2
     END)                                                    AS KOMPR_N  
FROM  thermiq_db.DATASTORE_RAW TR 
WHERE unix_timestamp(now()) - tid < 80 
  AND unix_timestamp(now()) - tid >= 20 
ORDER BY
   tid DESC 
LIMIT 1; ]]))

-- fetch the result
local row = assert(cursor:fetch ({}, "a"))

[..some processing stuff removed..]

-- close everything
print("Cursor: " .. tostring(cursor:close()))
print("Connection: " .. tostring(conn:close()))
print("Env: " .. tostring(env:close()))

local freeMem = collectgarbage('count');
print("GC Count : " .. freeMem/1024 .. " MB");

The memory allocation debug output shows that Lua constantly uses about 70 kB of memory.

Advice on what to change? Is more info needed to resolve the issue?