Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 81f1d99

Browse files
committed
Issue #143: UNIX domain methods, fix ResourceWarning and DeprecationWarning
warnings. create_unix_server() closes the socket on any error, not only on OSError.
1 parent 346ffb3 commit 81f1d99

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

asyncio/unix_events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,12 @@ def create_unix_connection(self, protocol_factory, path, *,
183183
raise ValueError(
184184
'path and sock can not be specified at the same time')
185185

186+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
186187
try:
187-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
188188
sock.setblocking(False)
189189
yield from self.sock_connect(sock, path)
190-
except OSError:
191-
if sock is not None:
192-
sock.close()
190+
except:
191+
sock.close()
193192
raise
194193

195194
else:
@@ -213,6 +212,7 @@ def create_unix_server(self, protocol_factory, path=None, *,
213212
try:
214213
sock.bind(path)
215214
except OSError as exc:
215+
sock.close()
216216
if exc.errno == errno.EADDRINUSE:
217217
# Let's improve the error message by adding
218218
# with what exact address it occurs.

tests/test_unix_events.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,17 @@ def test_create_unix_server_existing_path_sock(self):
221221
with test_utils.unix_socket_path() as path:
222222
sock = socket.socket(socket.AF_UNIX)
223223
sock.bind(path)
224-
225-
coro = self.loop.create_unix_server(lambda: None, path)
226-
with self.assertRaisesRegexp(OSError,
227-
'Address.*is already in use'):
228-
self.loop.run_until_complete(coro)
224+
with sock:
225+
coro = self.loop.create_unix_server(lambda: None, path)
226+
with self.assertRaisesRegex(OSError,
227+
'Address.*is already in use'):
228+
self.loop.run_until_complete(coro)
229229

230230
def test_create_unix_server_existing_path_nonsock(self):
231231
with tempfile.NamedTemporaryFile() as file:
232232
coro = self.loop.create_unix_server(lambda: None, file.name)
233-
with self.assertRaisesRegexp(OSError,
234-
'Address.*is already in use'):
233+
with self.assertRaisesRegex(OSError,
234+
'Address.*is already in use'):
235235
self.loop.run_until_complete(coro)
236236

237237
def test_create_unix_server_ssl_bool(self):
@@ -248,11 +248,13 @@ def test_create_unix_server_nopath_nosock(self):
248248
self.loop.run_until_complete(coro)
249249

250250
def test_create_unix_server_path_inetsock(self):
251-
coro = self.loop.create_unix_server(lambda: None, path=None,
252-
sock=socket.socket())
253-
with self.assertRaisesRegex(ValueError,
254-
'A UNIX Domain Socket was expected'):
255-
self.loop.run_until_complete(coro)
251+
sock = socket.socket()
252+
with sock:
253+
coro = self.loop.create_unix_server(lambda: None, path=None,
254+
sock=sock)
255+
with self.assertRaisesRegex(ValueError,
256+
'A UNIX Domain Socket was expected'):
257+
self.loop.run_until_complete(coro)
256258

257259
def test_create_unix_connection_path_sock(self):
258260
coro = self.loop.create_unix_connection(
@@ -278,7 +280,7 @@ def test_create_unix_connection_ssl_noserverhost(self):
278280
coro = self.loop.create_unix_connection(
279281
lambda: None, '/dev/null', ssl=True)
280282

281-
with self.assertRaisesRegexp(
283+
with self.assertRaisesRegex(
282284
ValueError, 'you have to pass server_hostname when using ssl'):
283285

284286
self.loop.run_until_complete(coro)

0 commit comments

Comments
 (0)