Skip to content

feat(server): construct the stack in a persistent event loop#2818

Merged
ashwinb merged 1 commit intollamastack:mainfrom
ashwinb:runloop
Jul 18, 2025
Merged

feat(server): construct the stack in a persistent event loop#2818
ashwinb merged 1 commit intollamastack:mainfrom
ashwinb:runloop

Conversation

@ashwinb
Copy link
Contributor

@ashwinb ashwinb commented Jul 18, 2025

When we call construct_stack(), providers are instantiated and initialize() is called. This call can end up doing anything at all -- specifically, providers are free to create long running background tasks as part of this. If we wrapped this within a asyncio.run() as in the current code, these tasks get canceled when the stack construction finishes. This is not correct. The PR addresses the issue by creating a persistent event loop which is used for both the stack as well as for running the uvicorn server. In other words, the lifetime of the providers (and downstream async code) is now the same as the lifetime of the uvicorn server.

Test Plan

This should not affect any current code since we don't have background tasks created right now. However, #2805 will start using this functionality.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 18, 2025
Copy link
Collaborator

@cdoern cdoern left a comment

Choose a reason for hiding this comment

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

this makes sense to me, one question

Copy link
Contributor

@ehhuang ehhuang left a comment

Choose a reason for hiding this comment

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

LG!

@ashwinb ashwinb merged commit 9e3ae50 into llamastack:main Jul 18, 2025
132 of 133 checks passed
@ashwinb ashwinb deleted the runloop branch July 18, 2025 17:29
leseb added a commit to leseb/llama-stack that referenced this pull request Jul 21, 2025
After llamastack#2818, SIGINT will
print a stack trace. This is because uvicorn re-raises SIGINT and it
gets converted by Python internal signal handler (default handles
SIGINT) to KeyboardInterrupt exception. We know simply catch the
exception to get a clean exit, this is not changing the behavior on
SIGINT.

Signed-off-by: Sébastien Han <seb@redhat.com>
ashwinb pushed a commit that referenced this pull request Jul 21, 2025
# What does this PR do?

After #2818, SIGINT will
print a stack trace. This is because uvicorn re-raises SIGINT and it
gets converted by Python internal signal handler (default handles
SIGINT) to KeyboardInterrupt exception. We know simply catch the
exception to get a clean exit, this is not changing the behavior on
SIGINT.

## Test Plan

Run the server, hit Ctrl+C or `kill -2 <server pid>` and expect a clean
exit with no stack trace.

Signed-off-by: Sébastien Han <seb@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants