Skip to content

Ensure connect callback is invoked on premature socket hangup #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 6, 2014
Merged
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
8 changes: 8 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,17 @@ Client.prototype.connect = function(callback) {
return self.emit('error', error);
}
callback(error);
callback = null;
});

con.once('end', function() {
if ( callback ) {
// haven't received a connection message yet !
var err = new Error("Stream unexpectedly ended before getting ready for query execution");
callback(err);
callback = null;
return;
}
if(self.activeQuery) {
var disconnectError = new Error('Stream unexpectedly ended during query execution');
self.activeQuery.handleError(disconnectError);
Expand Down
5 changes: 4 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ Connection.prototype.connect = function(port, host) {
self.emit('error', error);
});

this.stream.on('end', function() {
this.stream.on('close', function() {
// NOTE: node-0.10 emits both 'end' and 'close'
// for streams closed by the peer, while
// node-0.8 only emits 'close'
self.emit('end');
});

Expand Down
22 changes: 22 additions & 0 deletions test/unit/client/early-disconnect-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var helper = require(__dirname + '/test-helper');
var net = require('net');
var pg = require('../../..//lib/index.js');

var server = net.createServer(function(c) {
console.log('server connected');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome test! Love this. 💙

c.destroy();
console.log('server socket destroyed.');
server.close(function() { console.log('server closed'); });
});

server.listen(7777, function() {
console.log('server listening');
var client = new pg.Client('postgres://localhost:7777');
console.log('client connecting');
client.connect(assert.calls(function(err) {
if (err) console.log("Error on connect: "+err);
else console.log('client connected');
assert(err);
}));

});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('emits end when not in query', function() {
assert.equal(client.queryQueue.length, 0);
assert(client.activeQuery, 'client should have issued query');
process.nextTick(function() {
stream.emit('end');
stream.emit('close');
});
});
});