From ebe698720a7590f6b52a1fe2d65d5ecb838e3e52 Mon Sep 17 00:00:00 2001 From: Andreas Stokholm Date: Sun, 10 Feb 2013 04:16:01 +0100 Subject: [PATCH] Fixed crash when server went away. --- lib/memcache.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..ec73a04 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -28,6 +28,7 @@ var tcp = require('net'), var crlf = "\r\n"; var crlf_len = crlf.length; +var self; var error_replies = ['ERROR', 'NOT_FOUND', 'CLIENT_ERROR', 'SERVER_ERROR']; @@ -47,7 +48,7 @@ util.inherits(Client, process.EventEmitter); Client.prototype.connect = function () { if (!this.conn) { this.conn = new tcp.createConnection(this.port, this.host); - var self = this; + self = this; this.conn.addListener("connect", function () { this.setTimeout(0); // try to stay connected. this.setNoDelay(); @@ -107,7 +108,12 @@ Client.prototype.dispatchHandles = function() { Client.prototype.query = function(query, type, callback) { this.callbacks.push({ type: type, fun: callback }); this.sends++; - this.conn.write(query + crlf); + if (this.conn !== null) { + this.conn.write(query + crlf); + } else { + // Server has crashed or in another way become unavailable, connection closed. + self.emit("close"); + } }; Client.prototype.close = function() {