Skip to content

Commit f28a30c

Browse files
committed
loop: Let 'create_server' fail on socket bind errors
1 parent 9008581 commit f28a30c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

tests/test_tcp.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ async def start_server_ephemeral_ports():
161161

162162
self.loop.run_until_complete(start_server_ephemeral_ports())
163163

164+
def test_create_server_4(self):
165+
sock = socket.socket()
166+
sock.bind(('127.0.0.1', 0))
167+
168+
with sock:
169+
addr = sock.getsockname()
170+
171+
with self.assertRaisesRegex(OSError,
172+
"error while attempting.*\('127.*: "
173+
"address already in use"):
174+
175+
self.loop.run_until_complete(
176+
self.loop.create_server(object, *addr))
177+
164178
def test_create_connection_1(self):
165179
CNT = 0
166180
TOTAL_CNT = 100

uvloop/loop.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,9 +1069,12 @@ cdef class Loop:
10691069
try:
10701070
tcp.bind(addrinfo.ai_addr)
10711071
tcp.listen(backlog)
1072-
except:
1073-
tcp._close()
1074-
raise
1072+
except OSError as err:
1073+
pyaddr = __convert_sockaddr_to_pyaddr(
1074+
<system.sockaddr*>addrinfo.ai_addr)
1075+
raise OSError(err.errno, 'error while attempting '
1076+
'to bind on address %r: %s'
1077+
% (pyaddr, err.strerror.lower()))
10751078

10761079
server._add_server(tcp)
10771080

0 commit comments

Comments
 (0)