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

Commit 806cc68

Browse files
committed
Fixes #425 Remove UNIX socket from FS before binding.
1 parent 955f5ed commit 806cc68

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

asyncio/unix_events.py

+11
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ def create_unix_server(self, protocol_factory, path=None, *,
258258

259259
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
260260

261+
# Check for abstract socket. `str` and `bytes` paths are supported.
262+
if path[0] not in (0, '\x00'):
263+
try:
264+
if stat.S_ISSOCK(os.stat(path).st_mode):
265+
os.remove(path)
266+
except FileNotFoundError:
267+
pass
268+
except OSError as err:
269+
# Directory may have permissions only to create socket.
270+
logger.error('Unable to check or remove stale UNIX socket %r: %r', path, err)
271+
261272
try:
262273
sock.bind(path)
263274
except OSError as exc:

tests/test_unix_events.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ def test_create_unix_server_existing_path_sock(self):
241241
with test_utils.unix_socket_path() as path:
242242
sock = socket.socket(socket.AF_UNIX)
243243
sock.bind(path)
244-
with sock:
245-
coro = self.loop.create_unix_server(lambda: None, path)
246-
with self.assertRaisesRegex(OSError,
247-
'Address.*is already in use'):
248-
self.loop.run_until_complete(coro)
244+
sock.listen(1)
245+
sock.close()
246+
247+
coro = self.loop.create_unix_server(lambda: None, path)
248+
self.loop.run_until_complete(coro)
249249

250250
def test_create_unix_server_existing_path_nonsock(self):
251251
with tempfile.NamedTemporaryFile() as file:

0 commit comments

Comments
 (0)