summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Bar-Lev <alon.barlev@gmail.com>2013-05-31 22:21:01 +0300
committerAlon Bar-Lev <alon.barlev@gmail.com>2013-10-14 22:17:19 +0300
commitc2a40d69006516cf820953e2eec6854296f00b07 (patch)
tree2333cd77b8cf1c7b919df6286a7073471ef447ad
parent1190fe12041e1155bdf9871e2839b7aeddc94e33 (diff)
downloadwebsockify-c2a40d69006516cf820953e2eec6854296f00b07.tar.gz
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>
-rw-r--r--websockify/websocket.py24
1 files changed, 15 insertions, 9 deletions
diff --git a/websockify/websocket.py b/websockify/websocket.py
index 6aac338..1921cf9 100644
--- a/websockify/websocket.py
+++ b/websockify/websocket.py
@@ -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:
- _, exc, _ = sys.exc_info()
- print("In KeyboardInterrupt")
- pass
- except SystemExit:
+ except (self.Terminate, SystemExit, KeyboardInterrupt):
_, exc, _ = sys.exc_info()
- print("In SystemExit")
+ print("In exit")
break
except Exception:
_, exc, _ = sys.exc_info()