From c49206e1291a3db3a939284bd0c3eb551780e86b Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 30 Sep 2014 16:13:17 -0700 Subject: [PATCH] close websocket if proxyReq is closed before upgrade avoids leaving client sockets open when upstream connections are rejected. --- lib/http-proxy/passes/ws-incoming.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/http-proxy/passes/ws-incoming.js b/lib/http-proxy/passes/ws-incoming.js index 1bc786980..7c0fc72b0 100644 --- a/lib/http-proxy/passes/ws-incoming.js +++ b/lib/http-proxy/passes/ws-incoming.js @@ -89,8 +89,19 @@ var passes = exports; ); // Error Handler proxyReq.on('error', onOutgoingError); + var upgraded = false; + proxyReq.on('close', function () { + if (!upgraded) { + try { + socket.end(); + } catch (e) { + // ignore failure to teardown socket + } + } + }); proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) { + upgraded = true; proxySocket.on('error', onOutgoingError); // The pipe below will end proxySocket if socket closes cleanly, but not // if it errors (eg, vanishes from the net and starts returning