Closed as not planned
Closed as not planned
Description
Bug report
Bug description:
This code snippet should work:
async def amain():
logging.basicConfig(level=logging.DEBUG)
logging.info("Start server")
server = await asyncio.start_unix_server(
handle_client, path=SOCK_PATH, start_serving=False, backlog=10
)
logging.info("Open connection")
cli_r, cli_w = await asyncio.open_unix_connection(SOCK_PATH)
logging.info("Running")
await server.serve_forever()
However, the asyncio.open_unix_connection()
call fails with ConnectionRefusedError: [Errno 111] Connection refused
, as if the underlying socket wasn't listening for connections.
Output:
INFO:root:Start server
INFO:root:Open connection
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/src/__main__.py", line 47, in <module>
main()
File "/src/__main__.py", line 44, in main
asyncio.run(amain())
File "/usr/local/lib/python3.13/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/asyncio/base_events.py", line 709, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/src/__main__.py", line 27, in amain
cli_r, cli_w = await asyncio.open_unix_connection(SOCK_PATH)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/asyncio/streams.py", line 97, in open_unix_connection
transport, _ = await loop.create_unix_connection(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.13/asyncio/unix_events.py", line 263, in create_unix_connection
await self.sock_connect(sock, path)
File "/usr/local/lib/python3.13/asyncio/selector_events.py", line 641, in sock_connect
return await fut
^^^^^^^^^
File "/usr/local/lib/python3.13/asyncio/selector_events.py", line 649, in _sock_connect
sock.connect(address)
ConnectionRefusedError: [Errno 111] Connection refused
If we change the start_serving
parameter to True
, then the client connection will succeed.
However I believe that the socket listening and accepting connections should not depending entirely start_serving
.
Equivalent code with synchronous socket API, where the socket is in a listening state before asyncio.open_unix_connection()
async def amain():
logging.basicConfig(level=logging.DEBUG)
logging.info("Start server")
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM, 0)
with suppress(FileNotFoundError):
Path(SOCK_PATH).unlink()
sock.bind(SOCK_PATH)
sock.listen(10)
logging.info("Open connection")
cli_r, cli_w = await asyncio.open_unix_connection(SOCK_PATH)
logging.info("Running")
cli_sock, cli_addr = sock.accept()
logging.info("Client accepted !")
Output:
INFO:root:Start server
INFO:root:Open connection
INFO:root:Running
INFO:root:Client accepted !
Similar issues
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Metadata
Metadata
Assignees
Projects
Status
Done