-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memory leak at socket #1804
Comments
Solved. Adding the Connection:close header forces the server to disconnect after sending the reply and the leak is gone. replace the line: |
Thanks for posting the resolution, @gbechtinger. @djphoenix is this some kind of FAQ which should be added to the |
Just to clarify, using the connection:close header in this case, made the sck:close() useless,so I have removed it. I have to ignore the http module and use socket to the http request because http.get leaks memory when DNS failed to respond. |
Can't comment on a memory leak for failed DNS with |
No I didn't! This is the final code, if someone needs: srv = www.example.com (without http://) myhttp("www.dweet.io","/get/latest/dweet/for/yourdweetname",function(data)
if data ~= nil then print(data) end
end)
function myhttp(srv,getString,func)
net.dns.resolve(srv, function(dsck,ip)
if ip ~= nil then
dns[srv]=ip
end
local request = "GET http://"..srv..getString.." HTTP/1.1\r\nHost:"..wifi.sta.getip().."Content-Length:0\r\nConnection:close\r\n\r\n"
local conn2 = net.createConnection(net.TCP, 0)
conn2:on("connection", function(sck) sck:send(request) end)
conn2:on("receive", function(sck,c) func(c:match("\r\n\r\n(.-)$")) end)
conn2:connect(80,dns[srv])
end)
end |
As for the perceived memory leak we've been through this a few times e.g. just 3d ago. The key phrase seems to be "...and stabilizes" which points to the lw IP Also, it's true that the |
I'm also experiencing this using just |
I have the same problem of @coderkevin |
@coderkevin @felansu AFAIK the only way to fix that is to forget the HTTP module and do http request direcly over NET module. I'm doing that! |
The GET request in @gbechtinger example above is rather odd -- the standard HTTP request has the verb (GET) followed by the path of the URL (i.e. not including the scheme or server). The Host: header contains the server name and not the client address...... |
@pjsg @coderkevin @felansu @marcelstoer This is the final code of my http request, I forgot to upload the final version. Please note that If it can't connect, the code retry the request: I have notice that the Host header is ignored by some web servers that I use, but it needed when you acccess a server in a shared host enviroment like godaddy. That's why it worked with the client ip before. function myhttp(srv,getString,func,recon)
local sID = socketID
socketID = socketID+1
if recon then
print("Reconnecting to: "..srv)
end
net.dns.resolve(srv, function(dsck,ip)
if ip ~= nil then
dns[srv]=ip
end
local request = "GET http://"..srv..getString.." HTTP/1.1\r\nHost:"..srv.."\r\nContent-Length:0\r\nConnection:close\r\n\r\n"
local conn2 = net.createConnection(net.TCP, 0)
conn2:on("connection", function(sck) sck:send(request) end)
conn2:on("receive", function(sck,c)
if c~="0\r\n\r\n" then
func(c:match("\r\n\r\n(.-)$"))
end
end)
conn2:on("disconnection",function(sck,e) if e < 0 then myhttp(srv,getString,func,true) print("Socket disconnected") end end)
conn2:connect(80,dns[srv])
end)
end how to use: myhttp("www.site.com.br","/getCommand.php?username="..cfg[WEB_USERNAME].."&password="..cfg[WEB_PASSWORD],function(data)
--handle here the http reply as the data variable
end) |
I noticed this as well but didn't really want to contribute to a closed issue. Surprised this even works... It should be: -- srv could be replaced with dns[srv]
local request = "GET "..getString.." HTTP/1.1\r\nHost:"..srv.."Content-Length:0\r\nConnection:close\r\n\r\n" Also, I believe |
Sounds like maybe this issue should be re-opened or another one opened in it's place? |
I'm using 1.5, no errors, only bless !! |
I'm going to lock this conversation now before it meanders into even more directions. As I said in my initial comment this wasn't really a leak. It's just that due to the Sending the What Philipp and I last commented on was not the original closed issue but the oddities in the presented work-around code. And last but not least the |
Expected behavior
No memory leak
Actual behavior
Memory leak at every call, goes from 39488 free heap to 28040 (within 20-30 calls) )and stabilizes, and the leak stop. An average of 200 bytes per call. i'm using the 2.0.0 sdk
Test code
Provide a Minimal, Complete, and Verifiable example which will reproduce the problem.
NodeMCU version
Dev master from frightanic
Hardware
NODEMCU LOL1N
The text was updated successfully, but these errors were encountered: