Error deploying Gunicorn with FastAPI

I can deploy my app successfully using uvicorn, however when I try to deploy with gunicorn I get a repeating error. I am using the following to deploy with gunicorn:

gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:10000

Here is the error that keeps repeating:

[2024-03-15 23:23:10 +0000] [58] [INFO] Worker exiting (pid: 58)
[2024-03-15 23:23:11 +0000] [41] [ERROR] Worker (pid:56) exited with code 255
[2024-03-15 23:23:11 +0000] [41] [ERROR] Worker (pid:56) exited with code 255.
[2024-03-15 23:23:11 +0000] [59] [INFO] Booting worker with pid: 59
[2024-03-15 23:23:11 +0000] [59] [INFO] Started server process [59]
[2024-03-15 23:23:11 +0000] [59] [INFO] Waiting for application startup.
[2024-03-15 23:23:11 +0000] [59] [INFO] Application startup complete.
[2024-03-15 23:23:11 +0000] [59] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/selector_events.py", line 265, in _add_reader
    key = self._selector.get_key(fd)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/selectors.py", line 192, in get_key
    raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '9 is not registered'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
    worker.init_process()
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/workers.py", line 66, in init_process
    super(UvicornWorker, self).init_process()
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/gunicorn/workers/base.py", line 142, in init_process
    self.run()
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/workers.py", line 98, in run
    return asyncio.run(self._serve())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/nest_asyncio.py", line 31, in run
    return loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/nest_asyncio.py", line 99, in run_until_complete
    return f.result()
           ^^^^^^^^^^
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/futures.py", line 203, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/tasks.py", line 267, in __step
    result = coro.send(None)
             ^^^^^^^^^^^^^^^
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/workers.py", line 93, in _serve
    await server.serve(sockets=self.sockets)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/server.py", line 78, in serve
    await self.startup(sockets=sockets)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/server.py", line 128, in startup
    server = await loop.create_server(
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/base_events.py", line 1547, in create_server
    server._start_serving()
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/base_events.py", line 316, in _start_serving
    self._loop._start_serving(
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/selector_events.py", line 150, in _start_serving
    self._add_reader(sock.fileno(), self._accept_connection,
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/selector_events.py", line 267, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/selectors.py", line 359, in register
    self._selector.register(key.fd, poller_events)
FileExistsError: [Errno 17] File exists

Hi,

I’ve replied to the ticket you also opened. Let’s keep the conversation in one place (on the ticket). Then you can update this post when you have the solution.

Alan