From 806cc68d762acc30490cf5583569e41441c25c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA=20=28imac=29?= Date: Thu, 6 Oct 2016 14:30:30 +0500 Subject: [PATCH] Fixes #425 Remove UNIX socket from FS before binding. --- asyncio/unix_events.py | 11 +++++++++++ tests/test_unix_events.py | 10 +++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/asyncio/unix_events.py b/asyncio/unix_events.py index 42a8b859..65b61db6 100644 --- a/asyncio/unix_events.py +++ b/asyncio/unix_events.py @@ -258,6 +258,17 @@ def create_unix_server(self, protocol_factory, path=None, *, sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + # Check for abstract socket. `str` and `bytes` paths are supported. + if path[0] not in (0, '\x00'): + try: + if stat.S_ISSOCK(os.stat(path).st_mode): + os.remove(path) + except FileNotFoundError: + pass + except OSError as err: + # Directory may have permissions only to create socket. + logger.error('Unable to check or remove stale UNIX socket %r: %r', path, err) + try: sock.bind(path) except OSError as exc: diff --git a/tests/test_unix_events.py b/tests/test_unix_events.py index 0d54e3a6..015dbf97 100644 --- a/tests/test_unix_events.py +++ b/tests/test_unix_events.py @@ -241,11 +241,11 @@ def test_create_unix_server_existing_path_sock(self): with test_utils.unix_socket_path() as path: sock = socket.socket(socket.AF_UNIX) sock.bind(path) - with sock: - coro = self.loop.create_unix_server(lambda: None, path) - with self.assertRaisesRegex(OSError, - 'Address.*is already in use'): - self.loop.run_until_complete(coro) + sock.listen(1) + sock.close() + + coro = self.loop.create_unix_server(lambda: None, path) + self.loop.run_until_complete(coro) def test_create_unix_server_existing_path_nonsock(self): with tempfile.NamedTemporaryFile() as file: