# Buggy code
def _log_started_message(self, listeners: List[socket.SocketType]) -> None:
    config = self.config

    if config.fd is not None:
        sock = listeners[0]
        logger.info(
            "Uvicorn running on socket %s (Press CTRL+C to quit)",
            sock.getsockname(),
        )

    elif config.uds is not None:
        logger.info(
            "Uvicorn running on unix socket %s (Press CTRL+C to quit)", config.uds
        )

    else:
        addr_format = "%s://%s:%d"
        host = "0.0.0.0" if config.host is None else config.host
        if ":" in host:
            # It's an IPv6 address.
            addr_format = "%s://[%s]:%d"

        port = config.port
        if port == 0:
            port = listeners[0].getpeername()[1]

        protocol_name = "https" if config.ssl else "http"
        message = f"Uvicorn running on {addr_format} (Press CTRL+C to quit)"
        color_message = (
            "Uvicorn running on "
            + click.style(addr_format, bold=True)
            + " (Press CTRL+C to quit)"
        )
        logger.info(
            message,
            protocol_name,
            host,
            port,
            extra={"color_message": color_message},
        )


# Traceback (OSError)
[12:03:58] D:\sand\uvicorn-port-error
λ venv\win32-3.8\Scripts\uvicorn example:app --port=0
INFO:     Started server process [31268]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "D:\sand\uvicorn-port-error\venv\win32-3.8\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 782, in main
rv = self.invoke(ctx)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\main.py", line 362, in main
run(**kwargs)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\main.py", line 386, in run
server.run()
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 49, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 66, in serve
await self.startup(sockets=sockets)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 158, in startup
self._log_started_message(listeners)
File "d:\sand\uvicorn-port-error\venv\win32-3.8\lib\site-packages\uvicorn\server.py", line 190, in _log_started_message
port = listeners[0].getpeername()[1]
File "C:\Program Files\Python38\lib\asyncio\trsock.py", line 85, in getpeername
return self._sock.getpeername()
OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

# Fixed code
def _log_started_message(self, listeners: List[socket.SocketType]) -> None:
    config = self.config

    if config.fd is not None:
        sock = listeners[0]
        logger.info(
            "Uvicorn running on socket %s (Press CTRL+C to quit)",
            sock.getsockname(),
        )

    elif config.uds is not None:
        logger.info(
            "Uvicorn running on unix socket %s (Press CTRL+C to quit)", config.uds
        )

    else:
        addr_format = "%s://%s:%d"
        host = "0.0.0.0" if config.host is None else config.host
        if ":" in host:
            # It's an IPv6 address.
            addr_format = "%s://[%s]:%d"

        port = config.port
        if port == 0:
            port = listeners[0].getsockname()[1]

        protocol_name = "https" if config.ssl else "http"
        message = f"Uvicorn running on {addr_format} (Press CTRL+C to quit)"
        color_message = (
            "Uvicorn running on "
            + click.style(addr_format, bold=True)
            + " (Press CTRL+C to quit)"
        )
        logger.info(
            message,
            protocol_name,
            host,
            port,
            extra={"color_message": color_message},
        )