Skip to content

[Bug] ASGI 'lifespan' protocol appears unsupported #10018

Description

@ssrshz

Search before asking

  • I had searched in the issues and found no similar issues.

Apache SkyWalking Component

Python Agent (apache/skywalking-python)

What happened

It will have a issue when the app start with the Fastapi, as follows:

INFO: Finished server process [30539]
INFO: Started server process [30544]
INFO: Waiting for application startup.
INFO: ASGI 'lifespan' protocol appears unsupported.
INFO: Application startup complete.

and the startup and shutdown event will have no effect.

    @app.on_event("startup")
    async def init_connect():
        db.connect()

    @app.on_event('shutdown')
    async def shutdown_connect():
        if not db.is_closed():
            db.close()

So, i debug it, i find that the plugin 'sw_fastapi' doesn't consider the scenario when the scope type is not the http.This bug is not compatible with the uvicorn. When i add the follow code , it will work.

def install():
    from starlette.types import Receive, Scope, Send, Message
    from starlette.exceptions import ExceptionMiddleware

    _original_fast_api = ExceptionMiddleware.__call__

    def params_tostring(params):
        return '\n'.join([f"{k}=[{','.join(params.getlist(k))}]" for k, _ in params.items()])

    async def _sw_fast_api(self, scope: Scope, receive: Receive, send: Send):
        from starlette.requests import Request
      
        # add these code
        if scope["type"] != "http":
            await _original_fast_api(self, scope, receive, send)
            return

        req = Request(scope, receive=receive, send=send)
        carrier = Carrier()
        method = req.method

What you expected to happen

When i define the startup and shudown event ,they will do

How to reproduce

The scenario is as follows:
Fastapi 0.85.1
apache-skywalking 0.8.0

when you use the Fastapi startup and shudown event,it will occur

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions