Connections stay in CLOSE_WAIT with Node 0.10.x
After upgrading to Node 0.10 my servers keep accumulating TCP connections in the CLOSE_WAIT state until it hits the max-files limit.
Example server (based on the ws README example:
var WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
console.log('connection');
ws.send('something');
ws.on('close', function() {
console.log('close');
//ws.upgradeReq.socket.destroy();
});
ws.on('end', function() {
console.log('end');
});
});
- Run the above server
- Open Chrome console and execute
new WebSocket('ws://localhost:8080/');several times (I could not reproduce this issue withwscat). - Hit reload (all the connections close, you see some "close" message in the server output
lsof | grep CLOSE_WAITlists all the closed connections in CLOSE_WAIT state- Terminate the server
lsof | grep CLOSE_WAITis empty
If you modify the server and comment in the destroy line, there are no CLOSE_WAIT connections.
websocket.io does not seem to have this problem, as all sockets are destroyed (!) after close: https://github.com/LearnBoost/websocket.io/blob/master/lib/socket.js#L76
(while this is not nice at all, this workarounds the above problem)
The problem is Socket.end() called in cleanupWebsocketResource does not really close the socket. The socket stays half-open forever.
https://github.com/einaros/ws/blob/master/lib/WebSocket.js#L680
Maybe this should be if (!error) this._socket.destroy(); as we cannot hope for broken clients properly closing the connection (at least Chrome does not seem to do so)...
@nicokaiser did the change you suggested in #181 resolve this?
@3rd-Eden no, #181 did not help here. The destroy() call in #181 only kills broken connections, in this case the end() method is called (which does not end the socket).
@nicokaiser according to the docs, destroy should only be called when an error occurred. So I'm wondering if there's something else that keeps these sockets open (which would be a leak?).
Yes, I know. So there seems to be a leak. Can you test the server I posted above with Chrome as WebSocket client?
I'll try to write a test for the ws module, but the tests seem broken for me (DEPTH_ZERO_SELF_SIGNED_CERT)
I'm not running 0.10 yet, i'm waiting until the dreadfull crypto performance is fixed.
Hrm. Is this a node-in-general type issue? Have you seen any discussion on it elsewhere?
I don't know. Did not find anything usable in other bug reports, and I did not find any information if the Socket.end() behavior has changed since 0.8.
I'm facing a similar issue in my app. Node 0.10 HTTP Server(read: NOT WebSocketServer) starts leaving the connection in CLOSE_WAIT stage when I simulate a load of more than 1000 concurrent users. I'm using ab(apache bench) to simulate the concurrent requests to our node.js app.
I initially thought that it was a problem with our app leaking memory while accessing a Redis cluster and MySQL databases but then I tested this behavior with just a "Hello World" app and results into similar behavior.
There aren't any incompatible changes described in the upgrade list as well; https://github.com/joyent/node/wiki/API-changes-between-v0.8-and-v0.10
@isaacs are you aware of any issues like this?
@tarunkohli @3rd-Eden This sounds like potentially a bug in node itself. Can you please post your example program and test method in a new issue over at https://github.com/joyent/node/issues?
Was skimming through the node-redis changelog today and noticed; https://github.com/mranney/node_redis/issues/419 so i'm wondering if changing our stream.end calls to stream.destroySoon would resolve this.
@nicokaiser could you see if https://github.com/einaros/ws/blob/master/lib/WebSocket.js#L688 -> destroySoon resolves it for you?
@3rd-Eden net.Socket does not have destroySoon, does it?
Changing the socket.end calls to socket.destroy seems to solve the problem, however completely destroying the socket instead of cleanly ending them is not the nicest way, as @einaros said somewhere else...
@nicokaiser They do, but it's an internal method; https://github.com/joyent/node/blob/master/lib/net.js#L410-L418 what it does it try to end nicely when possible or destroy it when it's allowed to.
@3rd-Eden don't know if it's a good idea to rely on internal node methods here...
I know, but forcefully destroying a socket isn't either. — Twitter: @3rdEden Github: @3rd-Eden
On Wed, May 15, 2013 at 9:53 AM, Nico Kaiser [email protected] wrote:
@3rd-Eden don't know if it's a good idea to rely on internal node methods here...
Reply to this email directly or view it on GitHub: https://github.com/einaros/ws/issues/180#issuecomment-17924622
Using destroySoon is not a good idea. We've been planning for a long time to remove or rename that function, because it's really confusing. But the fact that it fixes the error is good information.
Again, can anyone point me at a minimized test case where this happens reliably? There's mention of such a thing in this thread, but I haven't been able to reproduce it. I'd really like to get this fixed properly.
I happened across this thread while trying to diagnose a similar issue.
FYI, this is also happening for me, except using http.request. All of my connections sit around in CLOSE_WAIT and it seems that I have to forcibly close them. So, maybe it is indeed a problem in node itself?
@brianseeders I'm sure that it is. Do you have a test case that can reproduce it? Are you the client or the server?
If you'er the client, what server are you connecting to? If you're the server, where are the connections coming from?
I'd love to fix this, but don't have anything more than anecdotal reports, so it's pretty hard to track down.