Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uvicorn worker are not taking ssl certfile from gunicorn config #3318

Open
Soufian20 opened this issue Oct 29, 2024 · 0 comments
Open

Uvicorn worker are not taking ssl certfile from gunicorn config #3318

Soufian20 opened this issue Oct 29, 2024 · 0 comments

Comments

@Soufian20
Copy link

Soufian20 commented Oct 29, 2024

I tried to configure gunicorn with uvicorn workler class to use SSL an recieved this exception

    run_gunicorn_app()
     └ <function run_gunicorn_app at 0x7f1d11e73640>
  File "/code/app/dotr_capi/_main.py", line 158, in run_gunicorn_app
     ).run()
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/app/base.py", line 71, in run
    Arbiter(self).run()
    │       └ <_main.GunicornApplication object at 0x7f1d16581720><class 'gunicorn.arbiter.Arbiter'>
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/arbiter.py", line 210, in run
     self.manage_workers()
     │    └ <function Arbiter.manage_workers at 0x7f1d13bd2440><gunicorn.arbiter.Arbiter object at 0x7f1d12828790>
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/arbiter.py", line 570, in manage_workers
     self.spawn_workers()
     │    └ <function Arbiter.spawn_workers at 0x7f1d13bd2560><gunicorn.arbiter.Arbiter object at 0x7f1d12828790>
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/arbiter.py", line 641, in spawn_workers
    self.spawn_worker()
    │    └ <function Arbiter.spawn_worker at 0x7f1d13bd24d0><gunicorn.arbiter.Arbiter object at 0x7f1d12828790>
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/arbiter.py", line 608, in spawn_worker
     worker.init_process()
     │      └ <function UvicornWorker.init_process at 0x7f1d11bf0040><uvicorn.workers.UvicornWorker object at 0x7f1d11c04ca0>
  File "/usr/local/lib/python3.10/dist-packages/uvicorn/workers.py", line 75, in init_process
    super().init_process()
   File "/usr/local/lib/python3.10/dist-packages/gunicorn/workers/base.py", line 143, in init_process
     self.run()
     │    └ <function UvicornWorker.run at 0x7f1d11bf0280><uvicorn.workers.UvicornWorker object at 0x7f1d11c04ca0>
   File "/usr/local/lib/python3.10/dist-packages/uvicorn/workers.py", line 107, in run
     return asyncio.run(self._serve())
           │       │   │    └ <function UvicornWorker._serve at 0x7f1d11bf01f0>
           │       │   └ <uvicorn.workers.UvicornWorker object at 0x7f1d11c04ca0>
            │       └ <function run at 0x7f1d16136b00><module 'asyncio' from '/usr/lib/python3.10/asyncio/__init__.py'>
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
     return loop.run_until_complete(main)
           │    │                  └ <coroutine object UvicornWorker._serve at 0x7f1d11c19e70>
           │    └ <cyfunction Loop.run_until_complete at 0x7f1d11c48ad0><uvloop.Loop running=False closed=True debug=False>
   File "uvloop/loop.pyx", line 1518, in uvloop.loop.Loop.run_until_complete
    return future.result()
   File "/usr/local/lib/python3.10/dist-packages/uvicorn/workers.py", line 102, in _serve
     await server.serve(sockets=self.sockets)
           │      │             │    └ [<gunicorn.sock.TCPSocket object at 0x7f1d157451e0>]
           │      │             └ <uvicorn.workers.UvicornWorker object at 0x7f1d11c04ca0>
         │      └ <function Server.serve at 0x7f1d157956c0><uvicorn.server.Server object at 0x7f1d11c04df0>
  File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 69, in serve
     await self._serve(sockets)
         │    │      └ [<gunicorn.sock.TCPSocket object at 0x7f1d157451e0>]
          │    └ <function Server._serve at 0x7f1d15795750><uvicorn.server.Server object at 0x7f1d11c04df0>
   File "/usr/local/lib/python3.10/dist-packages/uvicorn/server.py", line 76, in _serve
|     config.load()
    │      └ <function Config.load at 0x7f1d15748d30><uvicorn.config.Config object at 0x7f1d11c04b50>
   File "/usr/local/lib/python3.10/dist-packages/uvicorn/config.py", line 399, in load
    assert self.ssl_certfile
           │    └ None
          └ <uvicorn.config.Config object at 0x7f1d11c04b50>
 AssertionError: assert self.ssl_certfile
 2024-10-29 11:54:36.124 | INFO     | gunicorn.glogging:info:277 - Worker exiting (pid: 97)

For me it looks like uvicorn worker are not taken the ssl_certfile from the gunicorn config, because the assert to ssl_certfile is None.
This is my logic to start the application
`python
class GunicornApplication(BaseApplication):
def init(self, app, options=None):
self.options = options or {}
self.application = app
super().init()

def load_config(self):
    config = {
        key: value
        for key, value in self.options.items()
        if key in self.cfg.settings and value is not None
    }
    for key, value in config.items():
        self.cfg.set(key.lower(), value)

def load(self):
    return self.application

def run_gunicorn_app():
logger.info("Starting gunicorn process...")
GunicornApplication(
app,
options={
"bind": f"{SETTINGS.gunicorn.app_host}:{SETTINGS.gunicorn.app_port}",
"loglevel": SETTINGS.gunicorn.log_level,
"reload": SETTINGS.gunicorn.reload,
"workers": SETTINGS.gunicorn.workers,
"threads": SETTINGS.gunicorn.threads,
"timeout": SETTINGS.gunicorn.timeout,
"preload": SETTINGS.gunicorn.preload,
"keyfile": SETTINGS.gunicorn.keyfile,
"certfile ": SETTINGS.gunicorn.certfile,
"accesslog": "-",
"errorlog": "-",
"worker_class": "uvicorn.workers.UvicornWorker",
"logger_class": CustomGunicornLogger,
},
).run()
`
I start the app with python3 main.py in a docker container. I can confirm that mounting the certs file is fine, because when I configure in SETTINGS.gunicorn.certfile with a wrong path, i recieve a file not found error from gunicorn .

uvicorn code where the assertions happens -> https://github.com/encode/uvicorn/blob/fe3910083e3990695bc19c2ef671dd447262ae18/uvicorn/config.py#L399

gunicorn ssl docs -> https://docs.gunicorn.org/en/stable/settings.html#certfile

I am using:

  • uvicorn[standard]==0.32
  • gunicorn==23.0
  • fastapi==0.115

Someone facing the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant