Skip to content

Commit

Permalink
Added timeouts to prevent connection hanginng on connection loss
Browse files Browse the repository at this point in the history
  • Loading branch information
icook committed Mar 14, 2014
1 parent ea8c7f9 commit b397d4c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ppagent/main.py
Expand Up @@ -172,6 +172,7 @@ def collect(self):
def call(self, command, params=None):
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sok.connect((self.address, self.port))
sok.settimeout(5)
try:
sok.send('{"command":"' + command + '"}')
data = sok.makefile().readline()[:-1]
Expand Down Expand Up @@ -234,6 +235,7 @@ def connect(self):
.format(self.address, self.port))
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((self.address, self.port))
conn.settimeout(15)
self.conn = conn.makefile()
logger.debug("Announcing hello message")
self.conn.write(json.dumps({'method': 'hello', 'params': [0.1]}) + "\n")
Expand Down Expand Up @@ -263,7 +265,8 @@ def recieve(self):

try:
recv = self.conn.readline(4096)
except socket.error:
except socket.error as e:
logger.debug("Failed to recieve response, connection error", exc_info=True)
self.reset_connection()
return {}

Expand Down

0 comments on commit b397d4c

Please sign in to comment.