Skip to content

Conversation

objecthuman
Copy link
Contributor

Add Lifespan Events Handling in ASGI app


Description

This PR adds handling of lifespan events for the restate ASGI app.

Why is this change being introduced?
Any application that has any type of long-running network connection needs to close the connection that it created at startup. Currently the restate python sdk does not support handling of application lifespan. By adding support for lifespan handling, we allow application developers to close any long running connection that they might have opened.

Changes Introduced

Code Changes

  1. Add the lifespan_processor function to execute the lifespan function.
  2. Add unit tests for the lifespan_process function.
  3. Add CI to run the new unit tests

Example usage:

from contextlib import asynccontextmanager
import asyncio


@asynccontextmanager
async def lifespan_fn():
    print("perform startup tasks")
    yield
    print("perform shutdown tasks")
    await asyncio.sleep(1)

app = restate.app(services=[], lifespan=lifespan_fn)

@objecthuman
Copy link
Contributor Author

@igalshilman

@objecthuman
Copy link
Contributor Author

#105

@objecthuman objecthuman marked this pull request as ready for review July 23, 2025 05:58
@objecthuman
Copy link
Contributor Author

@slinkydeveloper

Copy link
Contributor

@igalshilman igalshilman left a comment

Choose a reason for hiding this comment

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

Hey, thank you for creating this PR, i did a first pass and it looks good!
I’d need to verify the lambda path + lifespan, we need to make sure that it still make sense.
Imagine a user that use aws lambda and they provide a lifespan.
Let me take another look later.

Copy link
Contributor

@igalshilman igalshilman left a comment

Choose a reason for hiding this comment

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

Thanks a lot @objecthuman, that looks like a great improvement.
I've tested this with hypercorn and uvicorn with and without a lifespan set,
and all seems to be working.

I have one question regarding state, and also an ask about adding event type assertions.

@objecthuman objecthuman requested a review from igalshilman July 31, 2025 04:30
Copy link
Contributor

@igalshilman igalshilman left a comment

Choose a reason for hiding this comment

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

Great work @objecthuman! I've tried this out locally with and without the lifespan both with unicorn and hypercorn and it works great.

@igalshilman
Copy link
Contributor

@objecthuman just before merging this, can you please rebase on main? it seems like there are some commits that are already in main.
Then I'd gladly merge.

@objecthuman
Copy link
Contributor Author

@objecthuman just before merging this, can you please rebase on main? it seems like there are some commits that are already in main. Then I'd gladly merge.
@igalshilman

image It's done already.

@igalshilman igalshilman merged commit 2aa17da into restatedev:main Aug 3, 2025
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants