websocket: restore signals after processing

WebSocketServer is a library module, as such it should try to restore state
after processing, to allow caller to resume normal operation.

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
This commit is contained in:
Alon Bar-Lev 2013-10-14 21:13:11 +03:00
parent 7026e26d68
commit 1190fe1204
1 changed files with 100 additions and 90 deletions

View File

@ -756,11 +756,17 @@ Sec-WebSocket-Accept: %s\r
self.started() # Some things need to happen after daemonizing self.started() # Some things need to happen after daemonizing
# Allow override of signals # Allow override of signals
original_signals = {
signal.SIGINT: signal.getsignal(signal.SIGINT),
signal.SIGTERM: signal.getsignal(signal.SIGTERM),
signal.SIGCHLD: signal.getsignal(signal.SIGCHLD),
}
signal.signal(signal.SIGINT, self.do_SIGINT) signal.signal(signal.SIGINT, self.do_SIGINT)
signal.signal(signal.SIGTERM, self.do_SIGTERM) signal.signal(signal.SIGTERM, self.do_SIGTERM)
signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD) signal.signal(signal.SIGCHLD, self.fallback_SIGCHLD)
last_active_time = self.launch_time last_active_time = self.launch_time
try:
while True: while True:
try: try:
try: try:
@ -857,12 +863,16 @@ Sec-WebSocket-Accept: %s\r
finally: finally:
if startsock: if startsock:
startsock.close() startsock.close()
finally:
# Close listen port # Close listen port
self.vmsg("Closing socket listening at %s:%s" self.vmsg("Closing socket listening at %s:%s"
% (self.listen_host, self.listen_port)) % (self.listen_host, self.listen_port))
lsock.close() lsock.close()
# Restore signals
for sig, func in original_signals.items():
signal.signal(sig, func)
# HTTP handler with WebSocket upgrade support # HTTP handler with WebSocket upgrade support
class WSRequestHandler(SimpleHTTPRequestHandler): class WSRequestHandler(SimpleHTTPRequestHandler):