]> granicus.if.org Git - python/commitdiff
Issue #28399: Remove UNIX socket from FS before binding.
authorYury Selivanov <yury@magic.io>
Sun, 9 Oct 2016 16:15:08 +0000 (12:15 -0400)
committerYury Selivanov <yury@magic.io>
Sun, 9 Oct 2016 16:15:08 +0000 (12:15 -0400)
Patch by Коренберг Марк.

Lib/asyncio/unix_events.py
Lib/test/test_asyncio/test_unix_events.py
Misc/NEWS

index 42a8b85981f54f996b861a9709f126529e784a6f..65b61db66ac74e53619bbf8b45890ce09017cad8 100644 (file)
@@ -258,6 +258,17 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
 
             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:
index 0d54e3a6d47fd103fc6d09021967aea0c45daea6..ce897ed6bdd03f4a5ee7c2516ee6df2959f80bc1 100644 (file)
@@ -241,11 +241,13 @@ class SelectorEventLoopUnixSocketTests(test_utils.TestCase):
         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)
+            srv = self.loop.run_until_complete(coro)
+            srv.close()
+            self.loop.run_until_complete(srv.wait_closed())
 
     def test_create_unix_server_existing_path_nonsock(self):
         with tempfile.NamedTemporaryFile() as file:
index db749913d1639060331e53d3a9d0abb94b3bc148..90eed7d5c98693d17f2297e66c598605845f6e68 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -385,6 +385,9 @@ Library
 
 - Issue #28372: Fix asyncio to support formatting of non-python coroutines.
 
+- Issue #28399: Remove UNIX socket from FS before binding.
+  Patch by Коренберг Марк.
+
 IDLE
 ----