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 0fb32f5283
commit 7084f43688
1 changed files with 11 additions and 3 deletions

View File

@ -98,6 +98,9 @@ Sec-WebSocket-Accept: %s\r
class CClose(Exception): class CClose(Exception):
pass pass
class Terminate(Exception):
pass
def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False, def __init__(self, listen_host='', listen_port=None, source_is_ipv6=False,
verbose=False, cert='', key='', ssl_only=None, verbose=False, cert='', key='', ssl_only=None,
daemon=False, record='', web='', daemon=False, record='', web='',
@ -751,6 +754,9 @@ Sec-WebSocket-Accept: %s\r
#self.vmsg("Running poll()") #self.vmsg("Running poll()")
pass pass
def terminate(self):
raise self.Terminate()
def fallback_SIGCHLD(self, sig, stack): def fallback_SIGCHLD(self, sig, stack):
# Reap zombies when using os.fork() (python 2.4) # Reap zombies when using os.fork() (python 2.4)
self.vmsg("Got SIGCHLD, reaping zombies") self.vmsg("Got SIGCHLD, reaping zombies")
@ -764,11 +770,11 @@ Sec-WebSocket-Accept: %s\r
def do_SIGINT(self, sig, stack): def do_SIGINT(self, sig, stack):
self.msg("Got SIGINT, exiting") self.msg("Got SIGINT, exiting")
sys.exit(0) self.terminate()
def do_SIGTERM(self, sig, stack): def do_SIGTERM(self, sig, stack):
self.msg("Got SIGTERM, exiting") self.msg("Got SIGTERM, exiting")
sys.exit(0) self.terminate()
def top_new_client(self, startsock, address): def top_new_client(self, startsock, address):
""" Do something with a WebSockets client connection. """ """ Do something with a WebSockets client connection. """
@ -893,6 +899,8 @@ Sec-WebSocket-Accept: %s\r
startsock, address = lsock.accept() startsock, address = lsock.accept()
else: else:
continue continue
except self.Terminate:
raise
except Exception: except Exception:
_, exc, _ = sys.exc_info() _, exc, _ = sys.exc_info()
if hasattr(exc, 'errno'): if hasattr(exc, 'errno'):
@ -937,7 +945,7 @@ Sec-WebSocket-Accept: %s\r
_, exc, _ = sys.exc_info() _, exc, _ = sys.exc_info()
print("In KeyboardInterrupt") print("In KeyboardInterrupt")
pass pass
except SystemExit: except (self.Terminate, SystemExit):
_, exc, _ = sys.exc_info() _, exc, _ = sys.exc_info()
print("In SystemExit") print("In SystemExit")
break break