Skip to content

Commit 78ba291

Browse files
committed
fixbug: exception on future timeout
1 parent 1413f61 commit 78ba291

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

p2p_python/core.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,20 @@ def get_my_user_header(self):
116116
'last_seen': int(time()),
117117
}
118118

119-
async def create_connection(self, host, port):
119+
async def create_connection(self, host, port) -> bool:
120+
"""create connection without exception"""
120121
if self.f_stop:
121122
return False
122123
# get connection list
123124
future: asyncio.Future = loop.run_in_executor(
124125
None, socket.getaddrinfo, host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
125126
try:
126127
await asyncio.wait_for(future, 10.0)
127-
except socket.gaierror:
128+
address_infos = future.result()
129+
except (asyncio.TimeoutError, socket.gaierror):
128130
return False
129131
# try to connect one by one
130-
for af, socktype, proto, canonname, host_port in future.result():
132+
for af, socktype, proto, canonname, host_port in address_infos:
131133
if host_port[0] in ban_address:
132134
return False # baned address
133135
try:
@@ -510,7 +512,7 @@ async def check_reachable(self, new_user: User):
510512
result = future.result()
511513
if result != 0:
512514
f_tcp = False
513-
except OSError:
515+
except (OSError, asyncio.TimeoutError):
514516
f_tcp = False
515517
loop.run_in_executor(None, sock.close)
516518
# try to check UDP

p2p_python/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ async def is_reachable(host, port):
109109
None, socket.getaddrinfo, host, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
110110
try:
111111
await asyncio.wait_for(future, 10.0)
112-
except socket.gaierror:
112+
addrs = future.result()
113+
except (socket.gaierror, asyncio.TimeoutError):
113114
return False
114-
for af, socktype, proto, canonname, host_port in future.result():
115+
for af, socktype, proto, canonname, host_port in addrs:
115116
try:
116117
sock = socket.socket(af, socktype, proto)
117118
except OSError:

0 commit comments

Comments
 (0)