Skip to content

Commit 6f82924

Browse files
committed
Respond to emptyQuery with a sync message
When a __prepared statement__ has no body in the query the backend responds with an `emptyQuery` message but never with a `commandComplete` or `errorResponse` message. The client was hanging forever waiting for one of the other two expected messages. The server was hanging forever waiting for the client to respond with a `sync` message. This change has the client send the required `sync` on receipt of an `emptyQuery` message when the query is a prepared statement. Fixes brianc#822
1 parent 7d534c2 commit 6f82924

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

lib/client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ Client.prototype.connect = function(callback) {
113113
self.activeQuery.handlePortalSuspended(con);
114114
});
115115

116+
//deletagate emptyQuery to active query
117+
con.on('emptyQuery', function(msg) {
118+
self.activeQuery.handleEmptyQuery(con);
119+
});
120+
116121
//delegate commandComplete to active query
117122
con.on('commandComplete', function(msg) {
118123
self.activeQuery.handleCommandComplete(msg, con);

lib/query.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ Query.prototype.handleCommandComplete = function(msg, con) {
7272
}
7373
};
7474

75+
//if a named prepared statement is created with empty query text
76+
//the backend will send an emptyQuery message but *not* a command complete message
77+
//execution on the connection will hang until the backend receives a sync message
78+
Query.prototype.handleEmptyQuery = function(con) {
79+
if (this.isPreparedStatement) {
80+
con.sync();
81+
}
82+
};
83+
7584
Query.prototype.handleReadyForQuery = function() {
7685
if(this._canceledDueToError) {
7786
return this.handleError(this._canceledDueToError);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//client should not hang on an empty query
2+
var helper = require('../test-helper');
3+
var client = helper.client();
4+
client.query({ name: 'foo1', text: null});
5+
client.query({ name: 'foo2', text: ' ' });
6+
client.query({ name: 'foo3', text: '' }, function(err, res) {
7+
client.end();
8+
});

0 commit comments

Comments
 (0)