websocket: do not exit at the middle of process

WebSocketServer is a library module, as such it should not exit process
but return from a method, allowing the caller to execute process show
down.

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
This commit is contained in:
Alon Bar-Lev 2013-05-31 22:21:01 +03:00
parent 1190fe1204
commit c2a40d6900
1 changed files with 15 additions and 9 deletions

View File

@ -90,6 +90,9 @@ Sec-WebSocket-Accept: %s\r
class CClose(Exception):
pass
class Terminate(Exception):
pass
def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
verbose=False, cert='', key='', ssl_only=None,
daemon=False, record='', web='', file_only=False, no_parent=False,
@ -665,6 +668,9 @@ Sec-WebSocket-Accept: %s\r
#self.vmsg("Running poll()")
pass
def terminate(self):
raise self.Terminate()
def fallback_SIGCHLD(self, sig, stack):
# Reap zombies when using os.fork() (python 2.4)
self.vmsg("Got SIGCHLD, reaping zombies")
@ -678,11 +684,11 @@ Sec-WebSocket-Accept: %s\r
def do_SIGINT(self, sig, stack):
self.msg("Got SIGINT, exiting")
sys.exit(0)
self.terminate()
def do_SIGTERM(self, sig, stack):
self.msg("Got SIGTERM, exiting")
sys.exit(0)
self.terminate()
def top_new_client(self, startsock, address):
""" Do something with a WebSockets client connection. """
@ -712,7 +718,7 @@ Sec-WebSocket-Accept: %s\r
self.ws_connection = True
self.new_client()
except self.CClose:
except self.CClose, WebSocketServer.Terminate:
# Close the client
_, exc, _ = sys.exc_info()
if self.client:
@ -722,6 +728,8 @@ Sec-WebSocket-Accept: %s\r
# Connection was not a WebSockets connection
if exc.args[0]:
self.msg("%s: %s" % (address[0], exc.args[0]))
except WebSocketServer.Terminate:
raise
except Exception:
_, exc, _ = sys.exc_info()
self.msg("handler exception: %s" % str(exc))
@ -806,6 +814,8 @@ Sec-WebSocket-Accept: %s\r
startsock, address = lsock.accept()
else:
continue
except self.Terminate:
raise
except Exception:
_, exc, _ = sys.exc_info()
if hasattr(exc, 'errno'):
@ -846,13 +856,9 @@ Sec-WebSocket-Accept: %s\r
# parent process
self.handler_id += 1
except KeyboardInterrupt:
except (self.Terminate, SystemExit, KeyboardInterrupt):
_, exc, _ = sys.exc_info()
print("In KeyboardInterrupt")
pass
except SystemExit:
_, exc, _ = sys.exc_info()
print("In SystemExit")
print("In exit")
break
except Exception:
_, exc, _ = sys.exc_info()