Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add no-op asgi-lifespan handling#856

Open
sheenobu wants to merge 1 commit intoprometheus:master from
sheenobu:lifespan-fixup
Open

Add no-op asgi-lifespan handling #856
sheenobu wants to merge 1 commit intoprometheus:master from
sheenobu:lifespan-fixup

Conversation

@sheenobu
Copy link

@sheenobu sheenobu commented Nov 16, 2022

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 sheena.artrip@gmail.com

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 <sheena.artrip@gmail.com>
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

Reviewers

@csmarchbanks csmarchbanks csmarchbanks left review comments

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ASGI lifespan crash

Comments

AltStyle によって変換されたページ (->オリジナル) /