From ea451344ffec241dc6a56fe461020401b4298b79 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 24 May 2014 19:16:21 +0800 Subject: [PATCH] fix workers --- .travis.yml | 1 + shadowsocks/server.py | 6 ++++++ shadowsocks/udprelay.py | 9 ++++++++- test/workers.json | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 20710fc..e0d10c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,4 @@ script: - python test.py -c test/aes.json - python test.py -c test/salsa20.json - python test.py -c test/server-multi-passwd.json + - python test.py -c test/workers.json diff --git a/shadowsocks/server.py b/shadowsocks/server.py index 67dfb55..29cd99e 100755 --- a/shadowsocks/server.py +++ b/shadowsocks/server.py @@ -299,7 +299,13 @@ def main(): sys.exit() import signal signal.signal(signal.SIGTERM, handler) + # master + for tcp_server in tcp_servers: + tcp_server.server_close() + for udp_server in udp_servers: + udp_server.close() + for child in children: os.waitpid(child, 0) else: diff --git a/shadowsocks/udprelay.py b/shadowsocks/udprelay.py index 5888918..6012661 100644 --- a/shadowsocks/udprelay.py +++ b/shadowsocks/udprelay.py @@ -137,6 +137,7 @@ class UDPRelay(object): self._cache = lru_cache.LRUCache(timeout=timeout, close_callback=self._close_client) self._client_fd_to_server_addr = lru_cache.LRUCache(timeout=timeout) + self._closed = False addrs = socket.getaddrinfo(self._listen_addr, self._listen_port, 0, socket.SOCK_DGRAM, socket.SOL_UDP) @@ -251,7 +252,7 @@ class UDPRelay(object): self._eventloop = eventloop.EventLoop() self._eventloop.add(server_socket, eventloop.POLL_IN) last_time = time.time() - while True: + while not self._closed: try: events = self._eventloop.poll(10) except (OSError, IOError) as e: @@ -274,11 +275,17 @@ class UDPRelay(object): last_time = now def start(self): + if self._closed: + raise Exception('closed') t = threading.Thread(target=self._run) t.setName('UDPThread') t.setDaemon(False) t.start() self._thread = t + def close(self): + self._closed = True + self._server_socket.close() + def thread(self): return self._thread diff --git a/test/workers.json b/test/workers.json index 4fd9d6b..c3d699b 100644 --- a/test/workers.json +++ b/test/workers.json @@ -6,5 +6,5 @@ "timeout":300, "method":"aes-256-cfb", "local_address":"127.0.0.1", - "workers": 2 + "workers": 4 }