Skip to content
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

Merged
merged 15 commits into from
Aug 8, 2024

Conversation

joerunde
Copy link
Collaborator

@joerunde joerunde commented Jul 19, 2024

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:

  • We adhere to Google Python style guide and Google C++ style guide.
  • Pass all linter checks. Please use format.sh to format your code.
  • The code need to be well-documented to ensure future contributors can easily understand the code.
  • Include sufficient tests to ensure the project to stay correct and robust. This includes both unit tests and integration tests.
  • Please add documentation to 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:

  • After the PR is submitted, the PR will be assigned to a reviewer. Every reviewer will pick up the PRs based on their expertise and availability.
  • After the PR is assigned, the reviewer will provide status update every 2-3 days. If the PR is not reviewed within 7 days, please feel free to ping the reviewer or the vLLM team.
  • After the review, the reviewer will put an 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.
  • Please respond to all comments within a reasonable time frame. If a comment isn't clear or you disagree with a suggestion, feel free to ask for clarification or discuss the suggestion.

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!

Copy link

👋 Hi! Thank you for contributing to the vLLM project.
Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which consists a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of default ones by unblocking the steps in your fast-check build on Buildkite UI.

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:

  • Comment /ready on the PR
  • Add ready label to the PR
  • Enable auto-merge.

🚀

@@ -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 "
Copy link
Collaborator Author

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)

Comment on lines 189 to 220
@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)

Copy link
Collaborator

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.

Copy link
Collaborator Author

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?

Copy link
Collaborator

@Yard1 Yard1 Jul 19, 2024

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

Copy link
Collaborator Author

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?

Copy link
Collaborator

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

Copy link
Contributor

@dtrifiro dtrifiro Jul 25, 2024

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

Copy link
Collaborator Author

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

@joerunde joerunde marked this pull request as ready for review July 22, 2024 23:04
@Yard1 Yard1 enabled auto-merge (squash) July 22, 2024 23:09
@github-actions github-actions bot added the ready ONLY add when PR is ready to merge/full CI is needed label Jul 22, 2024
@Yard1 Yard1 disabled auto-merge July 22, 2024 23:13
server = uvicorn.Server(config=uvicorn_config)

# Run the server and block until it exits
with contextlib.suppress(KeyboardInterrupt):
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Collaborator Author

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.

joerunde and others added 4 commits July 26, 2024 09:39
@joerunde
Copy link
Collaborator Author

@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?

@Yard1
Copy link
Collaborator

Yard1 commented Jul 26, 2024

@joerunde can you outline what you tried to do?

Signed-off-by: Joe Runde <[email protected]>
@vrdn-23
Copy link
Contributor

vrdn-23 commented Aug 1, 2024

@joerunde @Yard1 Is this PR good to merge? We've been experiencing some issues lately where the engine dies but the server is still up and it hence doesn't restart the Kubernetes pod on its own. I'm hoping this fix will help us mitigate this issue!

Signed-off-by: Joe Runde <[email protected]>
@joerunde
Copy link
Collaborator Author

joerunde commented Aug 1, 2024

@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:

  • Subclassing RemoteOpenAIServer to use fork and then mock.patching out the real model loads and injecting an exception into the LLMEngine, but just ran into too many small issues to debug
  • Running the equivalent of vllm serve in a separate thread to be able to mock out the llm engine in-process, but that turned into 150+ lines of code to maintain for this one little test which I also did not want to foist upon the rest of the maintainers

I see there were some other CI failures, I'll see if I can repro them or see if a rebase fixes it

Copy link
Member

@njhill njhill left a comment

Choose a reason for hiding this comment

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

Thanks @joerunde!

@@ -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",
Copy link
Member

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...

Copy link
Collaborator Author

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

@joerunde
Copy link
Collaborator Author

joerunde commented Aug 2, 2024

@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

@Yard1
Copy link
Collaborator

Yard1 commented Aug 5, 2024

@joerunde yeah let's fix the conflicts

@joerunde
Copy link
Collaborator Author

joerunde commented Aug 6, 2024

Cool, yeah with the addition of the common launcher I went ahead and moved these exception handlers there so that they can have access to the server to interrupt it, and I also had to fill out the rest of the interface for the new AsyncEngineRPCClient

@Yard1 Yard1 enabled auto-merge (squash) August 7, 2024 19:31
auto-merge was automatically disabled August 7, 2024 22:06

Head branch was pushed to by a user without write access

@joerunde
Copy link
Collaborator Author

joerunde commented Aug 7, 2024

Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
@joerunde
Copy link
Collaborator Author

joerunde commented Aug 8, 2024

@Yard1 @njhill I think we're up to date with ci passing, mind merging?

# 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)
Copy link
Member

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.

@njhill
Copy link
Member

njhill commented Aug 8, 2024

Thanks again @joerunde!

@njhill njhill merged commit 21b9c49 into vllm-project:main Aug 8, 2024
51 checks passed
@joerunde joerunde deleted the kill-the-server branch August 8, 2024 16:48
sfc-gh-mkeralapura pushed a commit to sfc-gh-mkeralapura/vllm that referenced this pull request Aug 12, 2024
kylesayrs pushed a commit to neuralmagic/vllm that referenced this pull request Aug 17, 2024
fialhocoelho pushed a commit to opendatahub-io/vllm that referenced this pull request Aug 22, 2024
Alvant pushed a commit to compressa-ai/vllm that referenced this pull request Oct 26, 2024
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Joe Runde <[email protected]>
Signed-off-by: Alvant <[email protected]>
KuntaiDu pushed a commit to KuntaiDu/vllm that referenced this pull request Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready ONLY add when PR is ready to merge/full CI is needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants