# 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.
        self.servers = []
        for sock in sockets:
            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"
        message = "Uvicorn running on %s://%s:%d (Press CTRL+C to quit)"
        color_message = (
            "Uvicorn running on "
            + click.style("%s://%s:%d", bold=True)
            + " (Press CTRL+C to quit)"
        )
        logger.info(
            message,
            protocol_name,
            config.host,
            port,
            extra={"color_message": color_message},
        )
        self.servers = [server]

    self.started = True


# Traceback (OSError)
INFO:     Uvicorn running on http://127.0.0.1:9000 (Press CTRL+C to quit)
INFO:     Started parent process [17696]
INFO:     Started server process [20428]
INFO:     Started server process [15476]
INFO:     Waiting for application startup.
INFO:     Waiting for application startup.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     ASGI 'lifespan' protocol appears unsupported.
INFO:     Application startup complete.
INFO:     Application startup complete.
Process SpawnProcess-2:
Traceback (most recent call last):
File "c:\users\arnab\appdata\local\programs\python\python37-32\Lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "c:\users\arnab\appdata\local\programs\python\python37-32\Lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "t:\my projects\scholify-web\env\lib\site-packages\uvicorn\subprocess.py", line 62, in subprocess_started
target(sockets=sockets)
File "t:\my projects\scholify-web\env\lib\site-packages\uvicorn\main.py", line 382, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "c:\users\arnab\appdata\local\programs\python\python37-32\Lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "t:\my projects\scholify-web\env\lib\site-packages\uvicorn\main.py", line 399, in serve
await self.startup(sockets=sockets)
File "t:\my projects\scholify-web\env\lib\site-packages\uvicorn\main.py", line 433, in startup
create_protocol, sock=sock, ssl=config.ssl, backlog=config.backlog
File "c:\users\arnab\appdata\local\programs\python\python37-32\Lib\asyncio\base_events.py", line 1393, in create_server
server._start_serving()
File "c:\users\arnab\appdata\local\programs\python\python37-32\Lib\asyncio\base_events.py", line 282, in _start_serving
sock.listen(self._backlog)
OSError: [WinError 10022] An invalid argument was supplied

# 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"
        message = "Uvicorn running on %s://%s:%d (Press CTRL+C to quit)"
        color_message = (
            "Uvicorn running on "
            + click.style("%s://%s:%d", bold=True)
            + " (Press CTRL+C to quit)"
        )
        logger.info(
            message,
            protocol_name,
            config.host,
            port,
            extra={"color_message": color_message},
        )
        self.servers = [server]

    self.started = True