Skip to content

Middleware : Exception caught where it souldn't #2074

@LiteApplication

Description

@LiteApplication

I encountered this issue that was hard to track down :

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/connexion/apps/abstract.py", line 288, in __call__
    return await self.middleware(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/connexion/middleware/main.py", line 501, in __call__
    self.app, self.middleware_stack = self._build_middleware_stack()
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/connexion/middleware/main.py", line 338, in _build_middleware_stack
    app.add_api(
  File "/app/.venv/lib/python3.11/site-packages/connexion/apps/flask.py", line 149, in add_api
    self.app.register_blueprint(api.blueprint)
  File "/app/.venv/lib/python3.11/site-packages/flask/sansio/scaffold.py", line 47, in wrapper_func
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/flask/sansio/app.py", line 595, in register_blueprint
    blueprint.register(self, options)
  File "/app/.venv/lib/python3.11/site-packages/flask/sansio/blueprints.py", line 310, in register
    raise ValueError(
ValueError: The name '/api' is already registered for a different blueprint. Use 'name=' to provide a unique name.

I think it comes down to the assignment not taking place because of an Exception raised and silently caught here (so self.middleware_stack stays None) :

self.app, self.middleware_stack = self._build_middleware_stack()

And I think that the error in question can be a KeyError here (debugger won't drill further down) :

There most likely was an error in my configuration, however I filled an issue because the cause is hidden by an exception caught wrongly which causes the library to misbehave in a way that is nowhere linked to the root cause of the issue.

Here is the KeyError :

Exception has occurred: KeyError
'scheme'
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/connexion/middleware/security.py", line 131, in __init__
    self.add_paths()
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/connexion/middleware/abstract.py", line 235, in add_api
    api = self.api_cls(specification, next_app=self.app, **kwargs)
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/connexion/middleware/main.py", line 338, in _build_middleware_stack
    app.add_api(
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/connexion/middleware/main.py", line 501, in __call__
    self.app, self.middleware_stack = self._build_middleware_stack()
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/connexion/apps/abstract.py", line 288, in __call__
    return await self.middleware(scope, receive, send)
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 29, in __call__
    return await self.app(scope, receive, send)
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/uvicorn/lifespan/on.py", line 86, in main
    await app(scope, self.receive, self.send)
  File "/usr/local/lib/python3.11/asyncio/events.py", line 84, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 1936, in _run_once
    handle._run()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 608, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.11/asyncio/base_events.py", line 641, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
  File "/usr/local/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/uvicorn/server.py", line 67, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/home/liteapp/adh6/api_server/.venv/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 80, in subprocess_started
    target(sockets=sockets)
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/usr/local/lib/python3.11/multiprocessing/spawn.py", line 135, in _main
    return self._bootstrap(parent_sentinel)
  File "/usr/local/lib/python3.11/multiprocessing/spawn.py", line 122, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "<string>", line 1, in <module>
KeyError: 'scheme'

(it is possible that some lines are off because it is running inside a docker environment and the debugger is outside)

Thank you for your time and contribution to the open source world 👍

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions