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

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/", line 265, in _add_reader
    key = self._selector.get_key(fd)
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/", 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/", line 609, in spawn_worker
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/", line 66, in init_process
    super(UvicornWorker, self).init_process()
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/gunicorn/workers/", line 142, in init_process
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/", line 98, in run
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/", line 31, in run
    return loop.run_until_complete(task)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/", line 99, in run_until_complete
    return f.result()
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/", 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/", line 267, in __step
    result = coro.send(None)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/", line 93, in _serve
    await server.serve(sockets=self.sockets)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/", line 78, in serve
    await self.startup(sockets=sockets)
  File "/opt/render/project/src/.venv/lib/python3.11/site-packages/uvicorn/", line 128, in startup
    server = await loop.create_server(
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/", line 1547, in create_server
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/", line 316, in _start_serving
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/asyncio/", 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/", line 267, in _add_reader
    self._selector.register(fd, selectors.EVENT_READ,
  File "/opt/render/project/python/Python-3.11.5/lib/python3.11/", line 359, in register
    self._selector.register(key.fd, poller_events)
FileExistsError: [Errno 17] File exists


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.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.