Skip to content

Commit 541f58d

Browse files
committed
5.6.1 refactor and fix bug of resource leaking.
1 parent 2dc1837 commit 541f58d

14 files changed

Lines changed: 238 additions & 117 deletions

File tree

code/default/launcher/web_control.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,6 @@ def req_debug_handler(self):
855855
dat += "thread num:%d\r\n" % threading.active_count()
856856
for thread in threading.enumerate():
857857
dat += "\nThread: %s \r\n" % (thread.name)
858-
# dat += traceback.format_exc(sys._current_frames()[thread.ident])
859858
stack = sys._current_frames()[thread.ident]
860859
st = traceback.extract_stack(stack)
861860
stl = traceback.format_list(st)

code/default/lib/noarch/front_base/boringssl_wrap.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self, context, sock, ip_str=None, sni=None, on_close=None):
1919
self._context = context
2020
self._sock = sock
2121
self._fileno = self._sock.fileno()
22+
# self._context.logger.debug("sock %s init fd:%d", ip_str, self._fileno)
2223
self.ip_str = utils.to_bytes(ip_str)
2324
self.sni = sni
2425
self._makefile_refs = 0
@@ -240,18 +241,49 @@ def close(self):
240241
self.running = False
241242
if not self.socket_closed:
242243
if self._connection:
243-
bssl.BSSL_SSL_shutdown(self._connection)
244+
res = bssl.BSSL_SSL_shutdown(self._connection)
245+
# res == 0: close_notify sent but not recv, means you need to call SSL_shutdown again if you want a full bidirectional shutdown.
246+
# res == 1: success, mean you previously received a close_notify alert from the other peer, and you're totally done
247+
# res == -1: failed
248+
# self._context.logger.debug("sock %s SSL_shutdown fd:%d res:%d", self.ip_str, self._fileno, res)
249+
250+
if res < 0:
251+
error = bssl.BSSL_SSL_get_error(self._connection, res)
252+
# self._context.logger.debug("sock %s shutdown fd:%d error:%d", self.ip_str, self._fileno, error)
253+
if error == 1:
254+
p = ffi.new("char[]",
255+
b"hello, worldhello, worldhello, worldhello, worldhello, world") # p is a 'char *'
256+
q = ffi.new("char **", p) # q is a 'char **'
257+
line_no = 0
258+
line_no_p = ffi.new("int *", line_no)
259+
error = bssl.BSSL_ERR_get_error_line(q, line_no_p)
260+
filename = ffi.string(q[0])
261+
# self._context.logger.error("error:%d file:%s, line:%s", error, filename, line_no_p[0])
262+
self._context.logger.debug("sock %s shutdown error: %s, file:%s, line:%d, sni:%s" %
263+
(self.ip_str, error, filename, line_no_p[0], self.sni))
264+
else:
265+
self._context.logger.debug("sock %s shutdown error:%s" % (self.ip_str, error))
266+
267+
bssl.BSSL_SSL_free(self._connection)
268+
self._connection = None
269+
270+
if self._sock:
271+
try:
272+
self._sock.close()
273+
# self._context.logger.debug("sock %s sock_close fd:%d", self.ip_str, self._fileno)
274+
except Exception as e:
275+
# self._context.logger.debug("sock %s sock_close fd:%d e:%r", self.ip_str, self._fileno, e)
276+
pass
277+
self._sock = None
244278

245279
self.socket_closed = True
280+
246281
if self._on_close:
247282
self._on_close(self.ip_str, self.sni)
283+
self._on_close = None
248284

249285
def __del__(self):
250286
self.close()
251-
if self._connection:
252-
bssl.BSSL_SSL_free(self._connection)
253-
self._connection = None
254-
self._sock = None
255287

256288
def settimeout(self, t):
257289
if not self.running:

code/default/lib/noarch/front_base/connect_manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ def _create_ssl_connection(self, ip_str, sni, host):
312312
t = threading.Thread(target=self._connect_ssl, args=fn_args, name="connect_ssl_%s" % ip_str)
313313
t.start()
314314
try:
315-
ssl_sock = q.get(timeout=3)
315+
ssl_sock = q.get(timeout=30)
316316
except:
317+
self.logger.warn("connect_ssl_timeout %s", ip_str)
317318
raise socket.error("timeout")
318319

319320
if not isinstance(ssl_sock, SSLConnection):

code/default/lib/noarch/front_base/http_common.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import threading
12
import time
23
import random
34

@@ -204,6 +205,8 @@ def __init__(self, logger, ip_manager, config, ssl_sock, close_cb, retry_task_cb
204205
self.retry_task_cb = retry_task_cb
205206
self.idle_cb = idle_cb
206207
self.log_debug_data = log_debug_data
208+
209+
self._lock = threading.Lock()
207210
self.accept_task = True
208211
self.keep_running = True
209212
self.processed_tasks = 0
@@ -215,6 +218,7 @@ def __init__(self, logger, ip_manager, config, ssl_sock, close_cb, retry_task_cb
215218
self.last_send_time = self.ssl_sock.create_time
216219
self.life_end_time = self.ssl_sock.create_time + \
217220
random.randint(self.config.connection_max_life, int(self.config.connection_max_life * 1.5))
221+
# self.logger.debug("worker.init %s", self.ip_str)
218222

219223
def __str__(self):
220224
o = ""
@@ -260,24 +264,30 @@ def update_debug_data(self, rtt, sent, received, speed):
260264
# else:
261265
# self.rtt = rtt
262266

263-
# self.log_debug_data(rtt, sent, received)
267+
self.log_debug_data(rtt, sent, received)
264268
return
265269

266270
def close(self, reason):
267-
if not self.keep_running:
268-
self.logger.warn("worker already closed %s", self.ip_str)
269-
return
270-
271-
self.accept_task = False
272-
self.keep_running = False
273-
self.ssl_sock.close()
274-
if reason not in ["idle timeout", "life end"]:
275-
now = time.time()
276-
inactive_time = now - self.last_recv_time
277-
if inactive_time < self.config.http2_ping_min_interval:
278-
self.logger.debug("%s worker close:%s inactive:%d", self.ip_str, reason, inactive_time)
279-
self.ip_manager.report_connect_closed(self.ssl_sock.ip_str, self.ssl_sock.sni, reason)
280-
self.close_cb(self)
271+
with self._lock:
272+
if not self.keep_running:
273+
# self.logger.warn("worker %s already closed %s", self.ip_str, reason)
274+
return
275+
276+
# self.logger.debug("worker.close %s reason:%s", self.ip_str, reason)
277+
self.accept_task = False
278+
self.keep_running = False
279+
self.ssl_sock.close()
280+
if reason not in ["idle timeout", "life end"]:
281+
now = time.time()
282+
inactive_time = now - self.last_recv_time
283+
if inactive_time < self.config.http2_ping_min_interval:
284+
self.logger.debug("%s worker close:%s inactive:%d", self.ip_str, reason, inactive_time)
285+
self.ip_manager.report_connect_closed(self.ssl_sock.ip_str, self.ssl_sock.sni, reason)
286+
self.close_cb(self)
287+
288+
def __del__(self):
289+
# self.logger.debug("__del__ %s", self.ip_str)
290+
self.close("__del__")
281291

282292
def get_score(self):
283293
# The smaller, the better

code/default/lib/noarch/simple_http_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ def __init__(self, sock):
129129
self.select2 = selectors.DefaultSelector()
130130
self.select2.register(sock, selectors.EVENT_READ)
131131

132+
def __del__(self):
133+
try:
134+
self.select2.unregister(self.sock)
135+
except:
136+
pass
137+
138+
try:
139+
socket.socket.close(self.sock)
140+
except:
141+
pass
142+
132143
def recv(self, to_read=8192, timeout=30.0):
133144
if timeout < 0:
134145
raise Exception("recv timeout")

code/default/lib/noarch/xlog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(self, name, buffer_size=0, file_name=None, roll_num=1,
2727
log_path=None, save_start_log=0, save_warning_log=False):
2828
self.name = str(name)
2929
self.file_max_size = 1024 * 1024
30-
self.buffer_lock = threading.Lock()
30+
self.buffer_lock = threading.RLock()
3131
self.buffer = {} # id => line
3232
self.buffer_size = buffer_size
3333
self.last_no = 0

code/default/smart_router/local/gfw_black_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,7 @@ google.ca
18381838
google.calstate.edu
18391839
google.cd
18401840
google.ci
1841+
google.cn
18411842
google.co.id
18421843
google.co.jp
18431844
google.co.kr

code/default/smart_router/local/smart_route.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,13 @@ def handle_domain_proxy(sock, host, port, client_address, left_buf=""):
580580
if not g.domain_cache.accept_gae(host):
581581
rule_list.remove("gae")
582582
elif g.config.country_code == "CN":
583-
if g.gfwlist.in_white_list(host):
584-
rule_list = ["direct", "gae", "socks", "redirect_https"]
585-
elif g.gfwlist.in_block_list(host):
583+
if g.gfwlist.in_block_list(host):
586584
if g.config.pac_policy == "black_X-Tunnel":
587585
rule_list = ["socks", "redirect_https", "direct", "gae"]
588586
else:
589587
rule_list = ["gae", "socks", "redirect_https", "direct"]
588+
elif g.gfwlist.in_white_list(host):
589+
rule_list = ["direct", "gae", "socks", "redirect_https"]
590590
else:
591591
ips = g.dns_query.query_recursively(host, 1)
592592
if g.ip_region.check_ips(ips):

code/default/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.6.0
1+
5.6.1

0 commit comments

Comments
 (0)