-->
Page 1 of 1

Memory leak TCP server?

PostPosted: Fri Apr 29, 2016 3:01 pm
by knack
Hello. I have a TCP/http server with memory leaks, every page request eat memory until the crash "not enough memory"

i try with a simple example i still have that memory leaks.
bug? missed something?

I build the firmware with online tool (dev branch) two days ago.

Code: Select allsrv = net.createServer(net.TCP)
srv:listen(80, function(c)
  c:on("receive", function(sck, pl)
    local resp = {}

    if (string.find(pl, "GET / HTTP/1.1") ~= nil
      or string.find(pl, "GET /index.") ~= nil)
    then
        resp[#resp + 1] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    elseif (string.find(pl, "GET /favicon.ico HTTP/1.1") ~= nil) then
        resp[#resp + 1] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    else
        resp[#resp + 1] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    end

    local function send()
      if #resp > 0
        then sck:send(table.remove(resp, 1))     
      else
        sck:close()
      end
    end
   
    sck:on("sent", send)
    send()
  end)
 
end)

Re: Memory leak TCP server?

PostPosted: Sun May 01, 2016 1:00 am
by Dsbaha
knack wrote:Hello. I have a TCP/http server with memory leaks, every page request eat memory until the crash "not enough memory"

i try with a simple example i still have that memory leaks.
bug? missed something?

I build the firmware with online tool (dev branch) two days ago.

Code: Select allsrv = net.createServer(net.TCP)
srv:listen(80, function(c)
  c:on("receive", function(sck, pl)
    local resp = {}

    if (string.find(pl, "GET / HTTP/1.1") ~= nil
      or string.find(pl, "GET /index.") ~= nil)
    then
        resp[#resp + 1] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    elseif (string.find(pl, "GET /favicon.ico HTTP/1.1") ~= nil) then
        resp[#resp + 1] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    else
        resp[#resp + 1] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\nServer: NodeMCU\r\nConnection: close\r\n\r\n"
    end

    local function send()
      if #resp > 0
        then sck:send(table.remove(resp, 1))     
      else
        sck:close()
      end
    end
   
    sck:on("sent", send)
    send()
  end)
 
end)


I concur, I believe I'm seeing a memory leak w/ TCP Server as well. Just the following causes the leak;

Code: Select allsrv = net.createServer(net.TCP)
srv:listen(80, function(conn)
  conn:on("receive", function(conn, payload)

  conn:send("HTTP/1.1 200 OK\r\nServer: nodemcu-httpserver\r\nContent-Type: text/xml\r\nCache-Control: 86400\r\nConnection: close\r\n\r\n")
  conn:on("sent", function(conn) print(node.heap()) conn:close() end)

  end)
end)

Re: Memory leak TCP server?

PostPosted: Wed Jun 22, 2016 5:52 am
by marcelstoer
The cause are closed upvalues. The "sent" callback function receives its own reference of net.socket as a parameter. You have to make sure that you use this one rather than the one received in the "listen" or "receive" callback. Replace
Code: Select allconn:on("sent", function(conn) print(node.heap()) conn:close() end)

with
Code: Select allconn:on("sent", function(sck) print(node.heap()) sck:close() end)

Look at the simple example in https://github.com/nodemcu/nodemcu-firm ... ming-model or the more elaborate one at http://nodemcu.readthedocs.io/en/dev/en ... socketsend. Unfortunately, a number of prominent resources still have the wrong example (README in master branch, Wikipedia, nodemcu.com).