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

Add no-op asgi-lifespan handling #856

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sheenobu
Copy link

Super simple hook that checks for lifespan and immediately responds type.complete, essentially making startup/shutdown lifespan events no-ops for the prometheus ASGI app.

Fixes #855

from hypercorn.middleware import DispatcherMiddleware
from prometheus_client import make_asgi_app
import asyncio

async def main():

    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    app = DispatcherMiddleware({
        "/metrics": make_asgi_app(),
    })


    config = Config()
    config.bind = ["localhost:8080"]

    await serve(app, config)

Before:

    assert scope.get("type") == "http"
AssertionError

After:

[2022-11-15 16:05:20 -0800] [59048] [INFO] Running on http://127.0.0.1:8080 (CTRL + C to quit)
INFO:hypercorn.error:Running on http://127.0.0.1:8080 (CTRL + C to quit)

Signed-off-by: Sheena Artrip [email protected]

Super simple hook that checks for lifespan and immediately responds type.complete, essentially making startup/shutdown lifespan events no-ops for the prometheus ASGI app.

Fixes prometheus#855

```
from hypercorn.middleware import DispatcherMiddleware
from prometheus_client import make_asgi_app
import asyncio

async def main():

    from hypercorn.asyncio import serve
    from hypercorn.config import Config

    app = DispatcherMiddleware({
        "/metrics": make_asgi_app(),
    })


    config = Config()
    config.bind = ["localhost:8080"]

    await serve(app, config)
```

Before:

```
    assert scope.get("type") == "http"
AssertionError
```

After:

```
[2022-11-15 16:05:20 -0800] [59048] [INFO] Running on http://127.0.0.1:8080 (CTRL + C to quit)
INFO:hypercorn.error:Running on http://127.0.0.1:8080 (CTRL + C to quit)
```

Signed-off-by: Sheena Artrip <[email protected]>
@sheenobu
Copy link
Author

If an exception is raised when calling the application callable with a lifespan.startup message or a scope with type lifespan, the server must continue but not send any lifespan events.

this might be a better solution!

Copy link
Member

@csmarchbanks csmarchbanks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an exception is raised when calling the application callable with a lifespan.startup message or a scope with type lifespan, the server must continue but not send any lifespan events.

That seems nice, could we just change the assert to instead throw an exception? That would be nice for if other types show up in the future.

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

Successfully merging this pull request may close these issues.

ASGI lifespan crash
2 participants