Skip to content

Commit

Permalink
Explicitly clean up connection
Browse files Browse the repository at this point in the history
Ensure the channel and the swift connection are always closed when
the transport thread is not live anymore.
  • Loading branch information
Juan J. Martinez committed May 13, 2014
1 parent 69d13c4 commit 7679c52
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions sftpcloudfs/server.py
Expand Up @@ -241,29 +241,33 @@ def handle(self):
# asynchronous negotiation with optional time limit; paramiko has a banner timeout already (15 secs)
start = time()
event = threading.Event()
t.start_server(server=self.server, event=event)
while True:
if event.wait(0.1):
if not t.is_active():
ex = t.get_exception() or "Negotiation failed."
self.log.warning("%r, disconnecting: %s" % (self.client_address, ex))
t.close()
try:
t.start_server(server=self.server, event=event)
while True:
if event.wait(0.1):
if not t.is_active():
ex = t.get_exception() or "Negotiation failed."
self.log.warning("%r, disconnecting: %s" % (self.client_address, ex))
return
self.log.debug("negotiation was OK")
break
if self.negotiation_timeout > 0 and time()-start > self.negotiation_timeout:
self.log.warning("%r, disconnecting: Negotiation timed out." % (self.client_address,))
return
self.log.debug("negotiation was OK")
break
if self.negotiation_timeout > 0 and time()-start > self.negotiation_timeout:
self.log.warning("%r, disconnecting: Negotiation timed out." % (self.client_address,))
t.close()

chan = t.accept(self.auth_timeout)
if chan is None:
self.log.warning("%r, disconnecting: auth failed, channel is None." % (self.client_address,))
return

chan = t.accept(self.auth_timeout)
if chan is None:
self.log.warning("%r, disconnecting: auth failed, channel is None." % (self.client_address,))
while t.isAlive():
t.join(timeout=10)
finally:
self.log.info("%r, cleaning up connection: bye." % (self.client_address,))
if self.server.fs.conn:
self.server.fs.conn.close()
t.close()
return

while t.isAlive():
t.join(timeout=10)
return

class ObjectStorageSFTPServer(ForkingTCPServer, paramiko.ServerInterface):
"""
Expand Down

0 comments on commit 7679c52

Please sign in to comment.