socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Memory leak: Disconnected socket stays in memory

Open thejh opened this issue 15 years ago • 6 comments

Hello, the following code leaks ca. 100KB (very roughly) of ram each time I press "reload" in the browser to reload the page and reconnect:

var lightnode = require('lightnode');
var http = require('http');

var fileServer = new lightnode.FileServer('/home/jann/gitty/node-assertvanish/');

var server = http.createServer(function(req, res) {
  fileServer.receiveRequest(req, res);
});

server.listen(8080, '0.0.0.0');
var io = require('socket.io').listen(server);

process.on('uncaughtException', function (err) {

        console.log('Exception: ' + err);

});

io.sockets.on('connection', function (socket) {

  socket.on('disconnect', function() {
    console.log("client gone");
  });

});

Modified version of the code for generating a memory backtrace Debug output of socket.io and a shortened memory backtrace (I filtered everything out that had an indentation level >12 and shortened the lines to 150 characters because gist didn't want to accept a 710K big file and because it's easier to read this way.)

As you can see, the socket is still referenced by the disconnect handler which in turn is referenced by the socket. It's not the reason of the leak. Therefore, what leaks is namespace.sockets.2644789511415902503. The namespace is also referenced by the second socket (which is alive) and the Manager, and because the namespace still has the dead socket in its sockets object, the dead socket stays in memory.

thejh avatar Jul 22 '11 18:07 thejh

+1

nookiepl avatar Jul 22 '11 18:07 nookiepl

This workaround seems to be working:

socket.on('disconnect', function() {
  delete socket.namespace.sockets[socket.id];
});

thejh avatar Jul 22 '11 19:07 thejh

is this related ???

https://github.com/LearnBoost/socket.io/issues/405

dymsza avatar Jul 24 '11 17:07 dymsza

@dymsza I don't think so, I think it's caused by something else.

3rd-Eden avatar Jul 27 '11 11:07 3rd-Eden

Ok, i have other question related to mem leak.

in manager.js we have

this.store.subscribe('disconnect', function (id) { self.onDisconnect(id); });

onDisconnect method is clean up method with clean

manager.handshaken , manager.open ,manager.connected ,manager.transports ,manager.closed , manager.rooms.

BUT:

i real cannot find place with call store.publish('disconnect'); i also used nodeJS inspector and put break point in this method but it look like this method is never run.

For me it look like memory leak.

Thanks,

I will be grateful for the quick reply thanks.

dymsza avatar Jul 27 '11 16:07 dymsza

Is this issue still existing? What is the fix?

martianmartian avatar Sep 15 '15 08:09 martianmartian