# Buggy code
async def startup(self, sockets=None):
    await self.lifespan.startup()
    if self.lifespan.should_exit:
        self.should_exit = True
        return

    config = self.config

    create_protocol = functools.partial(
        config.http_protocol_class, config=config, server_state=self.server_state
    )

    loop = asyncio.get_event_loop()

    if sockets is not None:
        # Explicitly passed a list of open sockets.
        # We use this when the server is run from a Gunicorn worker.

        def _share_socket(sock: socket) -> socket:
            # Windows requires the socket be explicitly shared across
            # multiple workers (processes).
            from socket import fromshare  # type: ignore

            sock_data = sock.share(os.getpid())  # type: ignore
            return fromshare(sock_data)

        self.servers = []
        for sock in sockets:
            if config.workers > 1 and platform.system() == "Windows":
                sock = _share_socket(sock)
            server = await loop.create_server(
                create_protocol, sock=sock, ssl=config.ssl, backlog=config.backlog
            )
            self.servers.append(server)

    elif config.fd is not None:
        # Use an existing socket, from a file descriptor.
        sock = socket.fromfd(config.fd, socket.AF_UNIX, socket.SOCK_STREAM)
        server = await loop.create_server(
            create_protocol, sock=sock, ssl=config.ssl, backlog=config.backlog
        )
        message = "Uvicorn running on socket %s (Press CTRL+C to quit)"
        logger.info(message % str(sock.getsockname()))
        self.servers = [server]

    elif config.uds is not None:
        # Create a socket using UNIX domain socket.
        uds_perms = 0o666
        if os.path.exists(config.uds):
            uds_perms = os.stat(config.uds).st_mode
        server = await loop.create_unix_server(
            create_protocol, path=config.uds, ssl=config.ssl, backlog=config.backlog
        )
        os.chmod(config.uds, uds_perms)
        message = "Uvicorn running on unix socket %s (Press CTRL+C to quit)"
        logger.info(message % config.uds)
        self.servers = [server]

    else:
        # Standard case. Create a socket from a host/port pair.
        try:
            server = await loop.create_server(
                create_protocol,
                host=config.host,
                port=config.port,
                ssl=config.ssl,
                backlog=config.backlog,
            )
        except OSError as exc:
            logger.error(exc)
            await self.lifespan.shutdown()
            sys.exit(1)
        port = config.port
        if port == 0:
            port = server.sockets[0].getsockname()[1]
        protocol_name = "https" if config.ssl else "http"
        if isinstance(ip_address(config.host), IPv6Address):
            message, color_message = _get_server_start_message(_IPKind.IPv6)
        else:
            message, color_message = _get_server_start_message(_IPKind.IPv4)

        logger.info(
            message,
            protocol_name,
            config.host,
            port,
            extra={"color_message": color_message},
        )
        self.servers = [server]

    self.started = True


# Traceback (ValueError)
.py thon/debugpy/launcher 61232 -- /Users/paulafernandez/Sandbox/test/test/start
INFO:     Started server process [43171]
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
cli.main()
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/Users/paulafernandez/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 267, in run_file
runpy.run_path(options.target, run_name=compat.force_str("__main__"))
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/paulafernandez/Sandbox/test/test/start.py", line 18, in <module>
uvicorn.run(
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 391, in run
server.run()
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 419, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 436, in serve
await self.startup(sockets=sockets)
File "/Users/paulafernandez/Library/Caches/pypoetry/virtualenvs/test-8kdwd4GS-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 526, in startup
if isinstance(ip_address(config.host), IPv6Address):
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ipaddress.py", line 53, in ip_address
raise ValueError('%r does not appear to be an IPv4 or IPv6 address' %
ValueError: 'localhost' does not appear to be an IPv4 or IPv6 address

# Fixed code
async def startup(self, sockets=None):
    await self.lifespan.startup()
    if self.lifespan.should_exit:
        self.should_exit = True
        return

    config = self.config

    create_protocol = functools.partial(
        config.http_protocol_class, config=config, server_state=self.server_state
    )

    loop = asyncio.get_event_loop()

    if sockets is not None:
        # Explicitly passed a list of open sockets.
        # We use this when the server is run from a Gunicorn worker.

        def _share_socket(sock: socket) -> socket:
            # Windows requires the socket be explicitly shared across
            # multiple workers (processes).
            from socket import fromshare  # type: ignore

            sock_data = sock.share(os.getpid())  # type: ignore
            return fromshare(sock_data)

        self.servers = []
        for sock in sockets:
            if config.workers > 1 and platform.system() == "Windows":
                sock = _share_socket(sock)
            server = await loop.create_server(
                create_protocol, sock=sock, ssl=config.ssl, backlog=config.backlog
            )
            self.servers.append(server)

    elif config.fd is not None:
        # Use an existing socket, from a file descriptor.
        sock = socket.fromfd(config.fd, socket.AF_UNIX, socket.SOCK_STREAM)
        server = await loop.create_server(
            create_protocol, sock=sock, ssl=config.ssl, backlog=config.backlog
        )
        message = "Uvicorn running on socket %s (Press CTRL+C to quit)"
        logger.info(message % str(sock.getsockname()))
        self.servers = [server]

    elif config.uds is not None:
        # Create a socket using UNIX domain socket.
        uds_perms = 0o666
        if os.path.exists(config.uds):
            uds_perms = os.stat(config.uds).st_mode
        server = await loop.create_unix_server(
            create_protocol, path=config.uds, ssl=config.ssl, backlog=config.backlog
        )
        os.chmod(config.uds, uds_perms)
        message = "Uvicorn running on unix socket %s (Press CTRL+C to quit)"
        logger.info(message % config.uds)
        self.servers = [server]

    else:
        # Standard case. Create a socket from a host/port pair.
        try:
            server = await loop.create_server(
                create_protocol,
                host=config.host,
                port=config.port,
                ssl=config.ssl,
                backlog=config.backlog,
            )
        except OSError as exc:
            logger.error(exc)
            await self.lifespan.shutdown()
            sys.exit(1)
        port = config.port
        if port == 0:
            port = server.sockets[0].getsockname()[1]
        protocol_name = "https" if config.ssl else "http"
        try:
            addr = ip_address(config.host)
            if isinstance(addr, IPv6Address):
                message, color_message = _get_server_start_message(is_ipv6_message=True)
            elif isinstance(addr, IPv4Address):
                message, color_message = _get_server_start_message()
        except ValueError:
            message, color_message = _get_server_start_message()

        logger.info(
            message,
            protocol_name,
            config.host,
            port,
            extra={"color_message": color_message},
        )
        self.servers = [server]

    self.started = True