Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56065d4

Browse files
ovvmiss-islington
authored andcommitted
bpo-34139: Remove unix datagram socket from FS before binding (GH-8323)
https://bugs.python.org/issue34139
1 parent 5aaac94 commit 56065d4

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

Lib/asyncio/base_events.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import itertools
2121
import os
2222
import socket
23+
import stat
2324
import subprocess
2425
import threading
2526
import time
@@ -1183,6 +1184,19 @@ async def create_datagram_endpoint(self, protocol_factory,
11831184
for addr in (local_addr, remote_addr):
11841185
if addr is not None and not isinstance(addr, str):
11851186
raise TypeError('string is expected')
1187+
1188+
if local_addr and local_addr[0] not in (0, '\x00'):
1189+
try:
1190+
if stat.S_ISSOCK(os.stat(local_addr).st_mode):
1191+
os.remove(local_addr)
1192+
except FileNotFoundError:
1193+
pass
1194+
except OSError as err:
1195+
# Directory may have permissions only to create socket.
1196+
logger.error('Unable to check or remove stale UNIX '
1197+
'socket %r: %r',
1198+
local_addr, err)
1199+
11861200
addr_pairs_info = (((family, proto),
11871201
(local_addr, remote_addr)), )
11881202
else:

Lib/test/test_asyncio/test_base_events.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,20 @@ def test_create_datagram_endpoint_sock_unix(self):
16621662
self.loop.run_until_complete(protocol.done)
16631663
self.assertEqual('CLOSED', protocol.state)
16641664

1665+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'No UNIX Sockets')
1666+
def test_create_datagram_endpoint_existing_sock_unix(self):
1667+
with test_utils.unix_socket_path() as path:
1668+
sock = socket.socket(socket.AF_UNIX, type=socket.SOCK_DGRAM)
1669+
sock.bind(path)
1670+
sock.close()
1671+
1672+
coro = self.loop.create_datagram_endpoint(
1673+
lambda: MyDatagramProto(create_future=True, loop=self.loop),
1674+
path, family=socket.AF_UNIX)
1675+
transport, protocol = self.loop.run_until_complete(coro)
1676+
transport.close()
1677+
self.loop.run_until_complete(protocol.done)
1678+
16651679
def test_create_datagram_endpoint_sock_sockopts(self):
16661680
class FakeSock:
16671681
type = socket.SOCK_DGRAM
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove stale unix datagram socket before binding

0 commit comments

Comments
 (0)