-1

I'm testing a little Lua web server that has an autoupdating-feature. The HTML code is redirecting to the web server itself every second. So the client's web browser always gets new data from the server instead of using the browser's cache.

If I connect with even only one client (my PC or my smartphone) after some time the NodeMCU-board crashes with this message:

PANIC: unprotected error in call to Lua API (SO-WebSrv-Test.lua:27: out of memory)

I used this code from Marcel Stoer who answered on a similar "running out of memory" question.

I modified Marcel's Lua code, but this code still eats up all heap memory over time.

I narrowed down the problem a little bit: if the refresh frequency of the HTML code is lower than 30 seconds the code eats up heap memory.

So how do I have to modify this code to achieve constant heap memory usage?

Best regards.

Stefan

tmr.alarm(0, 1000, 1, function()
   if wifi.sta.getip() == nil then
      print("trying to connect to AccessPoint...")
   else
      own_ip, netmask, gateway=wifi.sta.getip()
      print("connected to AccessPoint:")
      print("IP Info: \nIP Address of this device: ",own_ip)
      print("Netmask: ",netmask)
      print("Gateway Addr: ",gateway,"\n")
      print("type IP-Address "..own_ip.." into your browser to display SHT-31-website")
      tmr.stop(0)
   end
end)

counter = 0
srv = net.createServer(net.TCP, 28800)
print("Server created... \n")

srv:listen(80, function(conn)
    conn:on("receive", function(sck, request)
        local message = {}
        counter = counter + 1
        message[#message + 1] = "<head> <meta http-equiv=refresh content=1; URL=http://"..own_ip.."> </head>"
        message[#message + 1] = "<h1> ESP8266 SHT-31 Web Server Ver 003</h1>"
        message[#message + 1] = "<h2>some more text blabla blub"..counter.."</h2>"
        local function send(sk)
            if #message > 0 then
                sk:send(table.remove(message, 1))
            else
                sk:close()
                message = nil
                print("Heap Available:" .. node.heap())
            end
        end
        sck:on("sent", send)
        send(sck)
    end)
end)
dda
  • 6,030
  • 2
  • 25
  • 34
StefanL38
  • 9
  • 4
  • I have an idea what the root cause is. But first I suggest you delete the history in the description and just describe what the code is that you're _currently_ working with (and which fails AFAIU). – Marcel Stör Sep 18 '17 at 07:37

1 Answers1

1

You didn't tell us what firmware version you use. I tested a recent version in the Chromium browser and didn't see any memory problems. I aborted the test after 700+ reload cycles, heap consumption was absolutely stable.

Earlier this year we had to reduce the TCP TIME_WAIT parameter value because too many abandoned sockets kept in time-wait state were eating up memory. Explanation:

TIME-WAIT

(either server or client) represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. [According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as two MSL (maximum segment lifetime).]

Source: https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Protocol_operation

More details: https://www.rfc-editor.org/rfc/rfc7230#section-6.6

However:

  • you seem to intend to send HTML over HTTP back to your client but don't tell it, it may not like that
  • if your client (a browser?) doesn't close old sockets on time you may also tell it to do so explicitly

Adding proper HTTP headers fixes both. Hence, the message part should be like this:

local message = { "HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n" }

counter = counter + 1

message[#message + 1] = "<html><head> <meta http-equiv=refresh content=1; URL=http://" .. own_ip .. "> </head>"
message[#message + 1] = "<body><h1> ESP8266 SHT-31 Web Server Ver 003</h1>"
message[#message + 1] = "<h2>some more text blabla blub" .. counter .. "</h2></body></html>"

Take note of Connection: close and Content-Type: text/html as well as the properly structured HTML markup.

Community
  • 1
  • 1
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198