-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Frontend] Kill the server on engine death #6594
Conversation
👋 Hi! Thank you for contributing to the vLLM project. Once the PR is approved and ready to go, please make sure to run full CI as it is required to merge (or just use auto-merge). To run full CI, you can do one of these:
🚀 |
@@ -56,7 +56,7 @@ def _log_task_completion(task: asyncio.Task, | |||
error_callback(exception) | |||
raise AsyncEngineDeadError( | |||
"Task finished unexpectedly. This should never happen! " | |||
"Please open an issue on Github. See stack trace above for the" | |||
"Please open an issue on Github. See stack trace above for the " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(This has just triggered me every time I see the log)
@app.exception_handler(RuntimeError) | ||
async def runtime_error_handler(_, __): | ||
"""On generic runtime error, check to see if the engine has died. | ||
It probably has, in which case the server will no longer be able to | ||
handle requests. Trigger a graceful shutdown with a SIGTERM.""" | ||
if engine.errored and not engine.is_running: | ||
logger.fatal("AsyncLLMEngine has failed, terminating server " | ||
"process") | ||
os.kill(os.getpid(), signal.SIGTERM) | ||
|
||
return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR) | ||
|
||
@app.exception_handler(AsyncEngineDeadError) | ||
async def engine_dead_handler(_, __): | ||
"""Kill the server if the async engine is already dead. It will | ||
not handle any further requests.""" | ||
logger.fatal("AsyncLLMEngine is already dead, terminating server " | ||
"process") | ||
os.kill(os.getpid(), signal.SIGTERM) | ||
|
||
return Response(status_code=HTTPStatus.INTERNAL_SERVER_ERROR) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend gating this behavior behind an env var/argument, since automatic process death may be unexpected.
Why not just raise a normal exception, too? calling os.kill
on self seems like an anti-pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 I can gate that for sure. But I do think it should be the default behavior for running the server, I don't really see the utility in leaving it up and unresponsive if we know the engine is dead.
Why not just raise a normal exception, too?
In this case raising an exception isn't gonna kill the server, an unhandled exception in an endpoint handler will just cause the server to respond to the request with an empty 500 (which is also what I'm explicitly doing here). The main process is waiting on the server to exit, so the server has to be notified to exit somehow. AFAIK the canonical way of doing this when using uvicorn.run(...)
like we are is to use a termination signal. I agree that looks funky here though, It might be possible instead to do something like
server = uvicorn.Server(...)
server.run()
in our run_server
and then use server.shutdown()
in these handlers to try to initiate a shutdown that will then cause the main process to return from server.run()
and exit. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that sounds cleaner, but I don't have much experience with uvicorn, so take that with a grain of salt :)
I think it's ok if death is the default, but we should add a way to turn it off
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I think I'm happy with the server shutdown code now, and I added a flag to gate this logic.
I'm noodling on some tests and going down the route of running run_server()
in a thread with the llm engine patched to raise an error on each request, but it is looking a bit funky. Any other suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok for tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another approach could be scheduling a periodic task that checks the engine health and stops the http server task when the engine is unhealthy. See #6740
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dtrifiro true, though I think in this case we can avoid a lot of unnecessary checking since we catch the errors as they happen
be9908f
to
99e54d3
Compare
server = uvicorn.Server(config=uvicorn_config) | ||
|
||
# Run the server and block until it exits | ||
with contextlib.suppress(KeyboardInterrupt): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your changes overrode this- but this was here to stop an extra error from being printed when a user interrupts the server with CTRL+C. The uvicorn shutdown handlers still handle the signal properly, but the error would raise from here and be unhandled.
I think your changes to catch the server task being cancelled would handle this as well.
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
99e54d3
to
fc386c4
Compare
@Yard1 I banged my head against a wall for a few hours trying to unit test this without massively refactoring the server or injecting a poison pill into the engine to trigger an error, and I'm ready to give up 😦. Do you mind merging as-is without tests? |
@joerunde can you outline what you tried to do? |
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
@Yard1 @vrdn-23 Sorry for the delay, been focusing on #6883 instead. I went ahead and just wrote a simple test that uses a bad lora adapter to crash the server, which should at least work until that particular bug is fixed. I also tried both:
I see there were some other CI failures, I'll see if I can repro them or see if a rebase fixes it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @joerunde!
vllm/entrypoints/openai/cli_args.py
Outdated
@@ -134,6 +134,11 @@ def make_arg_parser(parser: FlexibleArgumentParser) -> FlexibleArgumentParser: | |||
help="When --max-logprobs is specified, represents single tokens as" | |||
"strings of the form 'token_id:{token_id}' so that tokens that" | |||
"are not JSON-encodable can be identified.") | |||
parser.add_argument("--keep-alive-on-engine-death", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only minor but I wonder if this would be better as an env var since I think it would only be used in debugging scenarios...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yeah I can change that. I only recently learned that there's an explicit split between the cli args and vllm.envs
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
@Yard1 It looks like the entrypoints tests pass, but other tests are failing with timeouts and it looks like I'd need to rebase main in order for the tracing tests to pass. Do you want me to rebase and re-trigger the CI? I don't want to use up too many CI runs for this one little PR |
@joerunde yeah let's fix the conflicts |
Cool, yeah with the addition of the common |
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
Head branch was pushed to by a user without write access
Added a little bugfix for an edge case I ran into after https://github.com/vllm-project/vllm/pull/7111/files#diff-4bd825485b5162cf8021da41a2ebd3d4026929e617d1137f69bbbe4bda0ee643R208 |
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
# Abort the request in the llm engine. | ||
await self.engine.abort(request.request_id) | ||
except Exception: | ||
logger.warning("Failed to abort request %s", request.request_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be good for this to be logger.exception(...)
so that the stacktrace is logged.
But don't want to trigger another whole round of CI tests just for this, we can address it in follow-on cleanup of the zmq decoupling that's being done anyhow.
Thanks again @joerunde! |
Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Alvant <[email protected]>
Signed-off-by: Joe Runde <[email protected]> Signed-off-by: Joe Runde <[email protected]>
Slight mitigation for #5901
This PR updates the open ai server to check for cases where the AsyncLLMEngine errors, and terminate itself when it does.
As I understand it, the goal of stopping the AsyncLLMEngine on unknown errors and not restarting is to prevent error loops where we try to restart the engine after an error that's really unrecoverable. However when we run into these issues in production, the server has to sit there responding with errors to all requests until a readiness probe fails, and then wait until the liveness probes fail enough times to trigger a container restart. This results longer downtime than necessary when we can instead terminate the server as soon as we know that the engine has failed.
This is implemented as a sigterm sent to the current PID which allows the uvicorn server to handle the signal and enter the usual graceful shutdown path.
There are two exception handlers added to the server. The first one checks for anything that extends
RuntimeError
, which I'm assuming will match most exceptions that kill the engine. This handler checks to see if the engine is dead, and kills the server if it is.The second handler matches the
AsyncEngineDeadError
explicitly and immediately kills the server, to catch any cases that were not caught by the first handler. This could happen if the engine was shared with another module that accidentally killed it (not speaking from experience here or anything 😉)PR Checklist (Click to Expand)
Thank you for your contribution to vLLM! Before submitting the pull request, please ensure the PR meets the following criteria. This helps vLLM maintain the code quality and improve the efficiency of the review process.
PR Title and Classification
Only specific types of PRs will be reviewed. The PR title is prefixed appropriately to indicate the type of change. Please use one of the following:
[Bugfix]
for bug fixes.[CI/Build]
for build or continuous integration improvements.[Doc]
for documentation fixes and improvements.[Model]
for adding a new model or improving an existing model. Model name should appear in the title.[Frontend]
For changes on the vLLM frontend (e.g., OpenAI API server,LLM
class, etc.)[Kernel]
for changes affecting CUDA kernels or other compute kernels.[Core]
for changes in the core vLLM logic (e.g.,LLMEngine
,AsyncLLMEngine
,Scheduler
, etc.)[Hardware][Vendor]
for hardware-specific changes. Vendor name should appear in the prefix (e.g.,[Hardware][AMD]
).[Misc]
for PRs that do not fit the above categories. Please use this sparingly.Note: If the PR spans more than one category, please include all relevant prefixes.
Code Quality
The PR need to meet the following code quality standards:
format.sh
to format your code.docs/source/
if the PR modifies the user-facing behaviors of vLLM. It helps vLLM user understand and utilize the new features or changes.Notes for Large Changes
Please keep the changes as concise as possible. For major architectural changes (>500 LOC excluding kernel/data/config/test), we would expect a GitHub issue (RFC) discussing the technical design and justification. Otherwise, we will tag it with
rfc-required
and might not go through the PR.What to Expect for the Reviews
The goal of the vLLM team is to be a transparent reviewing machine. We would like to make the review process transparent and efficient and make sure no contributor feel confused or frustrated. However, the vLLM team is small, so we need to prioritize some PRs over others. Here is what you can expect from the review process:
action-required
label on the PR if there are changes required. The contributor should address the comments and ping the reviewer to re-review the PR.Thank You
Finally, thank you for taking the time to read these guidelines and for your interest in contributing to vLLM. Your contributions make vLLM a great tool for everyone!