http 模块中的 NodeMCU 内存泄漏 (post)

NodeMCU memory leak in http module (post)

我可以使用以下代码可靠地重现 NodeMCU http 模块 (post) 中的内存泄漏:

msgData="This is some data that is being sent to the server; data is logged by the server"
function logWrite(str)
    str=tostring(tmr.time()).." -> "..tostring(node.heap()).." - "..str
    print(str)
end

function sendData()
    logWrite("sendData(sending)")

    http.post("http://192.168.2.38/DeviceScan.py", "Content-Type: text/plain\r\n", msgData,

      function(rspCode, payload)
          logWrite("sendData(http callback), rspCode="..tostring(rspCode))
          rspCode=nil
          payload=nil
      end)
end

status=tmr.alarm(1, 3000, tmr.ALARM_AUTO, sendData)

以上代码产生以下输出:

148 -> 43112 - sendData(sending)
148 -> 42240 - sendData(http callback), rspCode=200
151 -> 43112 - sendData(sending)
151 -> 42240 - sendData(http callback), rspCode=200
154 -> 43112 - sendData(sending)
154 -> 42240 - sendData(http callback), rspCode=200
157 -> 43112 - sendData(sending)
157 -> 42240 - sendData(http callback), rspCode=200
160 -> 43112 - sendData(sending)
160 -> 42240 - sendData(http callback), rspCode=200
163 -> 43112 - sendData(sending)
164 -> 42240 - sendData(http callback), rspCode=200
166 -> 43112 - sendData(sending)
166 -> 42648 - sendData(http callback), rspCode=-1
169 -> 42976 - sendData(sending)
169 -> 42496 - sendData(http callback), rspCode=-1
172 -> 42840 - sendData(sending)
172 -> 42368 - sendData(http callback), rspCode=-1
175 -> 42704 - sendData(sending)
175 -> 42216 - sendData(http callback), rspCode=-1

636 -> 24536 - sendData(http callback), rspCode=-1
639 -> 24872 - sendData(sending)
639 -> 24008 - sendData(http callback), rspCode=200
642 -> 24872 - sendData(sending)
642 -> 24008 - sendData(http callback), rspCode=200
645 -> 24872 - sendData(sending)
645 -> 24008 - sendData(http callback), rspCode=200
648 -> 24872 - sendData(sending)
648 -> 24008 - sendData(http callback), rspCode=200
651 -> 24872 - sendData(sending)
651 -> 24008 - sendData(http callback), rspCode=200
654 -> 24872 - sendData(sending)
654 -> 24008 - sendData(http callback), rspCode=200
657 -> 24872 - sendData(sending)
657 -> 24008 - sendData(http callback), rspCode=200
660 -> 24872 - sendData(sending)
660 -> 24008 - sendData(http callback), rspCode=200
663 -> 24872 - sendData(sending)
663 -> 24008 - sendData(http callback), rspCode=200
666 -> 24872 - sendData(sending)
666 -> 24008 - sendData(http callback), rspCode=200
669 -> 24872 - sendData(sending)
669 -> 24008 - sendData(http callback), rspCode=200
672 -> 24872 - sendData(sending)
672 -> 24008 - sendData(http callback), rspCode=200
675 -> 24872 - sendData(sending)
675 -> 24008 - sendData(http callback), rspCode=200
678 -> 24872 - sendData(sending)
678 -> 24536 - sendData(http callback), rspCode=-1
681 -> 24872 - sendData(sending)
681 -> 24400 - sendData(http callback), rspCode=-1
684 -> 24736 - sendData(sending)
684 -> 24264 - sendData(http callback), rspCode=-1
687 -> 24600 - sendData(sending)
687 -> 24128 - sendData(http callback), rspCode=-1
690 -> 24464 - sendData(sending)
690 -> 23992 - sendData(http callback), rspCode=-1
693 -> 24328 - sendData(sending)
693 -> 23856 - sendData(http callback), rspCode=-1

1240 -> 1688 - sendData(sending)
1240 -> 1216 - sendData(http callback), rspCode=-1
1243 -> 1552 - sendData(sending)
1243 -> 1080 - sendData(http callback), rspCode=-1
1246 -> 1416 - sendData(sending)
1246 -> 944 - sendData(http callback), rspCode=-1
E:M 768

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

当代码是第一个 运行 时,服务器已启动并且 运行ning 并且内存使用稳定。

就在时间戳 166 之前,服务器关闭(模拟 network/server 故障)并且可用内存开始减少。

就在时间戳 639 之前,服务器重新启动并且内存使用稳定。

就在时间戳 678 之前,服务器再次关闭,可用内存再次开始减少。可用内存持续减少,直到内存分配错误导致重置。

问题的一个线索是,每次 http.post 调用后内存减少与 http 请求的消息正文大小直接相关。邮件正文越大,可用内存减少的越多。

我正在使用以下版本:

NodeMCU custom build by frightanic.com
    branch: dev
    commit: c0f1e028a64d3aeb399a65b9c44eb512f21da62a
    SSL: true
    modules: file,gpio,http,net,node,rtctime,tmr,uart,wifi
 build  built on: 2016-05-16 19:30
 powered by Lua 5.1.4 on SDK 1.5.1(e67da894)

我还没有想出解决办法。这可以修复吗?

您的描述和可重现的测试用例中只有一个问题

Can this be fixed?

是的,我很确定它可以修复 ;-) - 在固件中。 https://github.com/nodemcu/nodemcu-firmware/issues/1383.

已经存在问题