[New-bugs-announce] [issue45693] `loop.create_server` with port=0 uses different ports for ipv4 & ipv6

Jim Crist-Harif report at bugs.python.org
Tue Nov 2 14:32:35 EDT 2021


New submission from Jim Crist-Harif <jiminy.crist at gmail.com>:

To create a new server with `loop.create_server` that listens on all interfaces and a random port, I'd expect passing in `host=""`, `port=0` to work (per the documentation). However, as written this results in 2 different ports being used - one for ipv4 and one for ipv6. Instead I'd expect a single random port be determined once, and reused for all other interfaces.

Running the example test code (attached) results in:

```
$ python test.py
listening on 0.0.0.0:38023
listening on :::40899
Traceback (most recent call last):
  File "/home/jcristharif/Code/distributed/test.py", line 36, in <module>
    asyncio.run(main())
  File "/home/jcristharif/miniconda3/envs/dask/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/home/jcristharif/miniconda3/envs/dask/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/home/jcristharif/Code/distributed/test.py", line 30, in main
    assert len(ports) == 1, "Only 1 port expected!"
AssertionError: Only 1 port expected!
```

This behavior can be worked around by manually handling `port=0` outside of asyncio, but as it stands naive use can result in accidentally listening on multiple ports.

----------
components: asyncio
files: test.py
messages: 405530
nosy: asvetlov, jcristharif, yselivanov
priority: normal
severity: normal
status: open
title: `loop.create_server` with port=0 uses different ports for ipv4 & ipv6
type: behavior
versions: Python 3.9
Added file: https://bugs.python.org/file50421/test.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45693>
_______________________________________


More information about the New-bugs-announce mailing list