async def application(scope, receive, send): if scope['type'] == 'lifespan': while True: message = await receive() if message['type'] == 'lifespan.startup': await send({"type": "lifespan.startup.failed"}) raise Exception('Exception blah') elif message['type'] == 'lifespan.shutdown': await send({'type': 'lifespan.shutdown.complete'}) return