Skip to content

FastAPI wrapper fails for requests from a unix socket. #103

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

Closed
theo-fokkinga opened this issue Jul 2, 2024 · 1 comment · Fixed by #107
Closed

FastAPI wrapper fails for requests from a unix socket. #103

theo-fokkinga opened this issue Jul 2, 2024 · 1 comment · Fixed by #107
Labels
bug Something isn't working

Comments

@theo-fokkinga
Copy link
Contributor

Describe the bug
We use a setup with a Gunicorn/FastAPI application binding to a unix socket. The FastAPI middleware from autodynatrace gives a TypeError: 'NoneType' object is not subscriptable on the scope["server"] variable.

Traceback (most recent call last):
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 428, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/fastapi/applications.py", line 1106, in __call__
    await super().__call__(scope, receive, send)
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/oracle_sdi/venv/lib64/python3.11/site-packages/autodynatrace/wrappers/fastapi/middleware.py", line 26, in __call__
    host = "{}:{}".format(scope["server"][0], scope["server"][1])
                          ~~~~~~~~~~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable

To Reproduce
Steps to reproduce the behavior:

  1. Configure Nginx to pass the requests to a unix socket
  2. Configure Gunicorn to bind to unix socket
  3. Configure Gunicorn with autodynatrace wrapper
  4. Start Gunicorn with a FastAPI application
  5. Send a request to Nginx
  6. Gunicorn will throw an Exception: [ERROR] Exception in ASGI application

Expected behavior
According to the ASGI specifications for http connection scope, the information on "server" is optional (https://asgi.readthedocs.io/en/latest/specs/www.html). However the implementation of the middleware wrapper for FastAPI (wrappers/fastapi/middleware.py) assumes the [host, port] iterable unconditionally.

Since the specification allows a None value and two-item iterable [path, None] too, these two cases need to be handled as well.

Example fix:

        host = ""
        # https://asgi.readthedocs.io/en/latest/specs/www.html
        # Optional; if missing defaults to None.
        if scope.get("server"):
            if scope["server"][1]:
                # [host:port]
                host = "{}:{}".format(scope["server"][0], scope["server"][1])
            else:
                # [path, None]
                host = "{}".format(scope["server"][0])

Server:

  • OS: Red Hat Enterprise Linux release 9.4 (Plow)
  • Python 3.11.7
  • autodynatrace 2.1.1
@theo-fokkinga theo-fokkinga added the bug Something isn't working label Jul 2, 2024
@theo-fokkinga
Copy link
Contributor Author

theo-fokkinga commented Jul 11, 2024

I've created a fix in PR #107

dlopes7 added a commit that referenced this issue Dec 2, 2024
Fix FastAPI wrapper for requests from a unix socket, fixes #103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant