Skip to content

Commit ebe6987

Browse files
Fixed crash when server went away.
1 parent b2482fe commit ebe6987

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
@@ -28,6 +28,7 @@ var tcp = require('net'),
2828

2929
var crlf = "\r\n";
3030
var crlf_len = crlf.length;
31+
var self;
3132

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

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

113119
Client.prototype.close = function() {

0 commit comments

Comments
 (0)