-->
Page 1 of 2

NodeMcu - closing socket

PostPosted: Fri Nov 21, 2014 2:59 am
by ThomasW
Hi

First - many thanks to the authors of NodeMcu, really promising!

Now my problem: The sample code below works fine but leaks about 100-300bytes of memory on every request - finally crashing the module. Is there a workaround or is this a bug? I know I could solve it by sending the "Content-length:" header but the 'real' server will serve dynamic content and it would be expensive to calculate the length in advance...

Thomas

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\n")
        -- conn:send("Content-length: 25\r\n")
        conn:send("Connection: close\r\n\r\n")
        conn:send("<h1> Hello, NodeMcu.</h1>")
        print(node.heap())
        conn:close()
    end)
end)

Re: NodeMcu - closing socket

PostPosted: Fri Nov 21, 2014 7:37 pm
by zioax
Probably it's a bug. I've tried also with the Content Length option but it crash also. The only mode for fix it maybe a fw update

Re: NodeMcu - closing socket

PostPosted: Sat Nov 22, 2014 4:55 am
by scargill
It is a bug. Strangely if you wait maybe a minute or two it recovers... But that kind of recovery needs to be instant.

Re: NodeMcu - closing socket

PostPosted: Sat Nov 22, 2014 7:09 am
by zeroday
ThomasW wrote:Hi

First - many thanks to the authors of NodeMcu, really promising!

Now my problem: The sample code below works fine but leaks about 100-300bytes of memory on every request - finally crashing the module. Is there a workaround or is this a bug? I know I could solve it by sending the "Content-length:" header but the 'real' server will serve dynamic content and it would be expensive to calculate the length in advance...

Thomas

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\n")
        -- conn:send("Content-length: 25\r\n")
        conn:send("Connection: close\r\n\r\n")
        conn:send("<h1> Hello, NodeMcu.</h1>")
        print(node.heap())
        conn:close()
    end)
end)


This is because Lua machine takes long time to collect memory(GC).
There is a
Code: Select allcollectgarbage("collect")
to force a GC in lua, but it seems not work...
If you wait like 1 minute. the ram will come back.
I am trying to find a way to collect all unused memory back immediately in Lua machine, I think I have to dive into the GC part.
That's not easy for me, so this bug will not be fixed quickly. :oops: