Skip to content

ASGI lifespan shutdown event is not sent on windows #772

@bbbergh

Description

@bbbergh

The shutdown event from the ASGI lifespan specification (https://asgi.readthedocs.io/en/latest/specs/lifespan.html) does not seem to be sent properly.

Steps to reproduce: Put the following code into test.py:

async def app(scope, receive, send):
    if scope['type'] == 'lifespan':
        while True:
            message = await receive()
            print(message)
    else:
        pass # Handle other types

and run with granian --interface asgi test.py:app, then interrupt the process with Ctrl+C. This will print

[INFO] Starting granian (main PID: 35828)
[INFO] Listening at: http://127.0.0.1:8000
[INFO] Spawning worker-1 with PID: 37184
{'type': 'lifespan.startup'}
[INFO] Shutting down granian

but there should be an additional {'type': 'lifespan.shutdown'} after pressing Ctrl+C (tested on Windows with python 3.14.0 and granian 2.6.0).

I don't have a good understanding of the code base, but the issue might be that the line

loop.run_until_complete(lifespan_handler.shutdown())

is not inside a finally block, and so will not be called if KeyboardInterrupt (or any other exception) is raised inside serve().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions