From 0b19b502fb7cfed4dca28b13018df91f448ef95d Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Tue, 27 Jan 2015 13:03:54 +0100 Subject: [PATCH] Catch exceptions in update_metric loop In order to prevent plugin crashing, it is necessary to catch select and fd syscals. See upstream python issue [7978] Thanks to @streadway for helping debug and fix this. [7978]: http://bugs.python.org/issue7978 --- memcached/python_modules/memcached.py | 34 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/memcached/python_modules/memcached.py b/memcached/python_modules/memcached.py index 07715458..f6bcff08 100755 --- a/memcached/python_modules/memcached.py +++ b/memcached/python_modules/memcached.py @@ -8,6 +8,7 @@ import time import socket import select +import errno descriptors = list() Desc_Skel = {} @@ -78,18 +79,27 @@ def update_metric(self): sock.send("stats\r\n") while True: - rfd, wfd, xfd = select.select([sock], [], [], self.timeout) - if not rfd: - print >>sys.stderr, "ERROR: select timeout" - break - - for fd in rfd: - if fd == sock: - data = fd.recv(8192) - msg += data - - if msg.find("END"): - break + try: + rfd, wfd, xfd = select.select([sock], [], [], self.timeout) + + if not rfd: + print >>sys.stderr, "ERROR: select timeout" + break + + for fd in rfd: + if fd == sock: + try: + data = fd.recv(8192) + msg += data + except (IOError, OSError), e: + if e.errno != errno.EINTR: + raise + + if msg.find("END"): + break + except select.error, e: + if e[0] != errno.EINTR: + raise sock.close() except socket.error, e: