Skip to content

Commit 7a57a9d

Browse files
authored
Merge pull request #1 from AndreasStokholm/master
Fixed crash when server went away.
2 parents 1ba2a3b + ebe6987 commit 7a57a9d

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/memcache.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var tcp = require('net'),
2929

3030
var crlf = "\r\n";
3131
var crlf_len = crlf.length;
32+
var self;
3233

3334
var error_replies = ['ERROR', 'NOT_FOUND', 'CLIENT_ERROR', 'SERVER_ERROR'];
3435

@@ -48,7 +49,7 @@ util.inherits(Client, EventEmitter);
4849
Client.prototype.connect = function () {
4950
if (!this.conn) {
5051
this.conn = new tcp.createConnection(this.port, this.host);
51-
var self = this;
52+
self = this;
5253
this.conn.addListener("connect", function () {
5354
this.setTimeout(0); // try to stay connected.
5455
this.setNoDelay();
@@ -108,7 +109,12 @@ Client.prototype.dispatchHandles = function() {
108109
Client.prototype.query = function(query, type, callback) {
109110
this.callbacks.push({ type: type, fun: callback });
110111
this.sends++;
111-
this.conn.write(query + crlf);
112+
if (this.conn !== null) {
113+
this.conn.write(query + crlf);
114+
} else {
115+
// Server has crashed or in another way become unavailable, connection closed.
116+
self.emit("close");
117+
}
112118
};
113119

114120
Client.prototype.close = function() {

0 commit comments

Comments
 (0)