Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,34 @@ function connect() {
};

const onConnected = function() {

if (originalSocketDestroy) {
socket.destroy = originalSocketDestroy;
}

cleanup();


// Keep the connect timeout armed through the STOMP handshake (do not
// clear it on transport connect). Otherwise a server that accepts the
// socket but never sends CONNECTED hangs forever with the socket open
// and the callback never fires. Cleared when the handshake settles.
client.removeListener('error', onError);
client.removeListener('connect', onConnected);

client.emit('socket-connect');

const connectOpts = Object.assign(
{host: options.host},
{host: options.host},
options.connectHeaders
);

client.connect(connectOpts, connectListener);

client.connect(connectOpts, function(error, connectedClient) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
if (typeof connectListener === 'function') {
connectListener(error, connectedClient);
}
});
};

let transportConnect = net.connect;
Expand Down
27 changes: 25 additions & 2 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ describe('connect(options, [connectionListener])', function(){
});

it('should callback on error', function(done){

var server = startBrokenServer();

connect({
host:'127.0.0.1',
port: server.address().port
Expand All @@ -95,6 +95,29 @@ describe('connect(options, [connectionListener])', function(){
done();
});
});

it('should time out when the STOMP handshake stalls after the transport connects', function(done){

// A server that accepts the TCP socket but never completes the STOMP handshake
// (never sends CONNECTED). Before the fix, the connect timeout was cleared once the
// transport connected, so this hung forever with the socket left open.
var sockets = [];
var server = net.createServer({family: 4}, function(socket){
sockets.push(socket); // hold the socket open, never respond
});
server.listen(0);

connect({
host: '127.0.0.1',
port: server.address().port,
timeout: 150
}, function(error){
assert(error, 'expected a connect timeout error');
sockets.forEach(function(s){ s.destroy(); });
server.close();
done();
});
});

it('should accept a transport connect function', function(done){

Expand Down