@@ -50,6 +50,7 @@ include "errors.pyx"
5050cdef:
5151 int PY39 = PY_VERSION_HEX >= 0x03090000
5252 int PY311 = PY_VERSION_HEX >= 0x030b0000
53+ int PY313 = PY_VERSION_HEX >= 0x030d0000
5354 uint64_t MAX_SLEEP = 3600 * 24 * 365 * 100
5455
5556
@@ -155,6 +156,8 @@ cdef class Loop:
155156 self ._fd_to_reader_fileobj = {}
156157 self ._fd_to_writer_fileobj = {}
157158
159+ self ._unix_server_sockets = {}
160+
158161 self ._timers = set ()
159162 self ._polls = {}
160163
@@ -1704,7 +1707,10 @@ cdef class Loop:
17041707 ' host/port and sock can not be specified at the same time' )
17051708 return await self .create_unix_server(
17061709 protocol_factory, sock = sock, backlog = backlog, ssl = ssl,
1707- start_serving = start_serving)
1710+ start_serving = start_serving,
1711+ # asyncio won't clean up socket file using create_server() API
1712+ cleanup_socket = False ,
1713+ )
17081714
17091715 server = Server(self )
17101716
@@ -2089,7 +2095,7 @@ cdef class Loop:
20892095 * , backlog = 100 , sock = None , ssl = None ,
20902096 ssl_handshake_timeout = None ,
20912097 ssl_shutdown_timeout = None ,
2092- start_serving = True ):
2098+ start_serving = True , cleanup_socket = PY313 ):
20932099 """ A coroutine which creates a UNIX Domain Socket server.
20942100
20952101 The return value is a Server object, which can be used to stop
@@ -2114,6 +2120,11 @@ cdef class Loop:
21142120 ssl_shutdown_timeout is the time in seconds that an SSL server
21152121 will wait for completion of the SSL shutdown before aborting the
21162122 connection. Default is 30s.
2123+
2124+ If *cleanup_socket* is true then the Unix socket will automatically
2125+ be removed from the filesystem when the server is closed, unless the
2126+ socket has been replaced after the server has been created.
2127+ This defaults to True on Python 3.13 and above, or False otherwise.
21172128 """
21182129 cdef:
21192130 UnixServer pipe
@@ -2191,6 +2202,15 @@ cdef class Loop:
21912202 # we want Python socket object to notice that.
21922203 sock.setblocking(False )
21932204
2205+ if cleanup_socket:
2206+ path = sock.getsockname()
2207+ # Check for abstract socket. `str` and `bytes` paths are supported.
2208+ if path[0 ] not in (0 , ' \x00 ' ):
2209+ try :
2210+ self ._unix_server_sockets[sock] = os_stat(path).st_ino
2211+ except FileNotFoundError:
2212+ pass
2213+
21942214 pipe = UnixServer.new(
21952215 self , protocol_factory, server, backlog,
21962216 ssl, ssl_handshake_timeout, ssl_shutdown_timeout)
0 commit comments