LearnBoost/socket.io — pull request #1064 by natefaubion (2012-10-30 00:09:11+00:00)
Title: Sessions leaking when using RedisStore

We've been trying to deploy Socket.IO in production, but have been having a hard time keeping it up for more than a couple hours due to ballooning memory usage. Looking at a heap dump showed that `manager.handshaken` was not getting cleared out. I put in some logging and found that other nodes were never getting notified of disconnects, and thus not clearing out their handshake data. I added another couple of publish calls in `onDisconnect` within the `if (local)` block to let other nodes know of the disconnects.

I also found that while in manager.js, `onDisconnect` has an argument `local`, it is actually `onClientDisconnect` that gets called with the local flag in transport.js. Consequently handlers were also never getting removed from the store. I added the flag to `onClientDisconnect` and pass it on to `onDisconnect`.

With some more logging, I found that some keys still weren't getting cleared out of transport, closed, and open (usually left with an empty array). So I added an additional routine to garbageCollection to loop through those and clear out any keys that weren't in `handshaken`.

These changes have kept memory usage from climbing so quickly, though it still continues to climb slowly (not sure where else to look at this point). CPU usage using RedisStore is the larger problem for us right now.