8000 gh-111246: Add opt-out of Unix socket cleanup (#111246) · python/cpython@93571fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 93571fe

Browse files
committed
gh-111246: Add opt-out of Unix socket cleanup (#111246)
Allow the implicit cleanup to be disabled in case there are some odd corner cases where this causes issues.
1 parent df3b74e commit 93571fe

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ Creating network servers
776776
*, sock=None, backlog=100, ssl=None, \
777777
ssl_handshake_timeout=None, \
778778
ssl_shutdown_timeout=None, \
779-
start_serving=True)
779+
start_serving=True, cleanup_socket=True)
780780
781781
Similar to :meth:`loop.create_server` but works with the
782782
:py:const:`~socket.AF_UNIX` socket family.
@@ -786,6 +786,10 @@ Creating network servers
786786
:class:`str`, :class:`bytes`, and :class:`~pathlib.Path` paths
787787
are supported.
788788

789+
If *cleanup_socket* is True then the Unix socket will automatically
790+
be removed from the filesystem when the server is closed, unless the
791+
socket has been replaced after the server has been created.
792+
789793
See the documentation of the :meth:`loop.create_server` method
790794
for information about arguments to this method.
791795

@@ -802,9 +806,7 @@ Creating network servers
802806

803807
.. versionchanged:: 3.13
804808

805-
The Unix socket will automatically be removed from the filesystem
806-
when the server is closed, unless the socket has been replaced
807-
after the server has been created.
809+
Added the *cleanup_socket* parameter.
808810

809811

810812
.. coroutinemethod:: loop.connect_accepted_socket(protocol_factory, \

Lib/asyncio/unix_events.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ async def create_unix_server(
285285
sock=None, backlog=100, ssl=None,
286286
ssl_handshake_timeout=None,
287287
ssl_shutdown_timeout=None,
288-
start_serving=True):
288+
start_serving=True, cleanup_socket=True):
289289
if isinstance(ssl, bool):
290290
raise TypeError('ssl argument must be an SSLContext or None')
291291

@@ -341,13 +341,14 @@ async def create_unix_server(
341341
raise ValueError(
342342
f'A UNIX Domain Stream Socket was expected, got {sock!r}')
343343

344-
path = sock.getsockname()
345-
# Check for abstract socket. `str` and `bytes` paths are supported.
346-
if path[0] not in (0, '\x00'):
347-
try:
348-
self._unix_server_sockets[sock] = os.stat(path).st_ino
349-
except FileNotFoundError:
350-
pass
344+
if cleanup_socket:
345+
path = sock.getsockname()
346+
# Check for abstract socket. `str` and `bytes` paths are supported.
347+
if path[0] not in (0, '\x00'):
348+
try:
349+
self._unix_server_sockets[sock] = os.stat(path).st_ino
350+
except FileNotFoundError:
351+
pass
351352

352353
sock.setblocking(False)
353354
server = base_events.Server(self, [sock], protocol_factory,

Lib/test/test_asyncio/test_server.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ async def serve(*args):
205205
srv.close()
206206
self.assertTrue(os.path.exists(addr))
207207

208+
@socket_helper.skip_unless_bind_unix_socket
209+
async def test_unix_server_cleanup_prevented(self):
210+
with test_utils.unix_socket_path() as addr:
211+
async def serve(*args):
212+
pass
213+
214+
srv = await asyncio.start_unix_server(serve, addr, cleanup_socket=False)
215+
216+
srv.close()
217+
self.assertTrue(os.path.exists(addr))
218+
208219

209220
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
210221
class ProactorStartServerTests(BaseStartServer, unittest.TestCase):

0 commit comments

Comments
 (0)
0