From 380a646f095e26bd1b27f557d6c7661813df2a6d Mon Sep 17 00:00:00 2001 From: clowwindy Date: Fri, 12 Sep 2014 22:18:08 +0800 Subject: [PATCH] handle signal soon; #179 --- shadowsocks/eventloop.py | 5 +++-- shadowsocks/tcprelay.py | 16 ++++++++-------- shadowsocks/udprelay.py | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/shadowsocks/eventloop.py b/shadowsocks/eventloop.py index 65f6473..a80a525 100644 --- a/shadowsocks/eventloop.py +++ b/shadowsocks/eventloop.py @@ -206,9 +206,10 @@ class EventLoop(object): events = self.poll(1) except (OSError, IOError) as e: if errno_from_exception(e) in (errno.EPIPE, errno.EINTR): - # Happens when the client closes the connection + # EPIPE: Happens when the client closes the connection + # EINTR: Happens when received a signal + # handles them as soon as possible logging.debug('poll:%s', e) - continue else: logging.error('poll:%s', e) import traceback diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index ccb74ee..372f1e2 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -632,14 +632,14 @@ class TCPRelay(object): if now - self._last_time > TIMEOUT_PRECISION: self._sweep_timeout() self._last_time = now - if self._closed: - if self._server_socket: - self._eventloop.remove(self._server_socket) - self._server_socket.close() - self._server_socket = None - logging.info('closed listen port %d', self._listen_port) - if not self._fd_to_handlers: - self._eventloop.remove_handler(self._handle_events) + if self._closed: + if self._server_socket: + self._eventloop.remove(self._server_socket) + self._server_socket.close() + self._server_socket = None + logging.info('closed listen port %d', self._listen_port) + if not self._fd_to_handlers: + self._eventloop.remove_handler(self._handle_events) def close(self, next_tick=False): self._closed = True diff --git a/shadowsocks/udprelay.py b/shadowsocks/udprelay.py index 89f3593..64d6a43 100644 --- a/shadowsocks/udprelay.py +++ b/shadowsocks/udprelay.py @@ -264,11 +264,11 @@ class UDPRelay(object): self._cache.sweep() self._client_fd_to_server_addr.sweep() self._last_time = now - if self._closed: - self._server_socket.close() - for sock in self._sockets: - sock.close() - self._eventloop.remove_handler(self._handle_events) + if self._closed: + self._server_socket.close() + for sock in self._sockets: + sock.close() + self._eventloop.remove_handler(self._handle_events) def close(self, next_tick=False): self._closed = True