FastDeploy icon indicating copy to clipboard operation
FastDeploy copied to clipboard

[Feature] Support stopping the inference for the corresponding request in the online service after a disconnection request.

Open qwes5s5 opened this issue 1 month ago • 2 comments

Motivation

:bulb: If this PR is a Cherry Pick, the PR title needs to follow the format by adding the [Cherry-Pick] label at the very beginning and appending the original PR ID at the end. For example, [Cherry-Pick][CI] Add check trigger and logic(#5191)

:bulb: 如若此PR是Cherry Pick,PR标题需遵循格式,在最开始加上[Cherry-Pick]标签,以及最后面加上原PR ID,例如[Cherry-Pick][CI] Add check trigger and logic(#5191)

In the current code structure, when a request disconnects from the online inference service, the resources and ongoing inference occupied by that request in FastDeploy are not immediately terminated or released. Therefore, corresponding logic needs to be implemented to capture the request interruption and release the associated resources to stop the inference.

Modifications

  • API Layer Unification (API 层的统一引入)

    • Decorator Definition and Application:
      • In the utils module, add or enhance the with_cancellation decorator to uniformly handle the cancellation logic for all requests (e.g., listening for HTTP disconnection).
      • Apply the @with_cancellation decorator uniformly to all external interfaces in the API layer (e.g., chat, completions, score, etc.).
  • Exception Handling and Notification (异常捕获与通知)

    • Exception Capture:
      • Within the Handler logic, capture the asyncio.CancelledError exception thrown by the @with_cancellation decorator.
    • ZMQ Communication:
      • After capturing the exception, use the ZeroMQ (zmq) message queue to send the relevant cancellation information to the backend Engine Service, notifying it to stop the inference task.
  • Engine Layer Cancellation Handling (EngineService) (引擎层中断处理)

    • Request Retrieval:
      • The EngineService modifies its receiving logic in the existing request thread to receive the cancellation request message via ZMQ.
    • Core Cancellation Function:
      • Introduce a new core function, abort_requests, specifically responsible for handling the received cancellation requests, implementing the stopping of inference and resource reclamation.
  • Resource Cleanup and Scheduling (abort_requests Details) (资源清理与调度)

    • Inside the abort_requests function, the following key steps will be executed to ensure the request is fully terminated and resources are recovered:
      1. Result Construction: Construct the final RequestOutput result and place it into the scheduler's output queue.
      2. Task Cleanup: Clear the corresponding tasks and flags for the request from the tasks_list and stop_flags structures.
      3. Resource Release:
        • Use the resource manager to release the KV Cache blocks occupied by the request.
        • Remove the request record from data structures such as the request dictionary.

Usage or Command

Accuracy Tests

Checklist

  • [x] Add at least a tag in the PR title.
    • Tag list: [[FDConfig],[APIServer],[Engine], [Scheduler], [PD Disaggregation], [Executor], [Graph Optimization], [Speculative Decoding], [RL], [Models], [Quantization], [Loader], [OP], [KVCache], [DataProcessor], [BugFix], [Docs], [CI], [Optimization], [Feature], [Benchmark], [Others], [XPU], [HPU], [GCU], [DCU], [Iluvatar], [Metax]]
    • You can add new tags based on the PR content, but the semantics must be clear.
  • [x] Format your code, run pre-commit before commit.
  • [ ] Add unit tests. Please write the reason in this PR if no unit tests.
  • [x] Provide accuracy results.
  • [x] If the current PR is submitting to the release branch, make sure the PR has been submitted to the develop branch, then cherry-pick it to the release branch with the [Cherry-Pick] PR tag.

qwes5s5 avatar Dec 01 '25 14:12 qwes5s5

Thanks for your contribution!

paddle-bot[bot] avatar Dec 01 '25 17:12 paddle-bot[bot]

Codecov Report

:x: Patch coverage is 89.00000% with 11 lines in your changes missing coverage. Please review. :warning: Please upload report for BASE (develop@8acdd9f). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/engine/common_engine.py 9.09% 10 Missing :warning:
fastdeploy/output/token_processor.py 96.29% 0 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #5320   +/-   ##
==========================================
  Coverage           ?   65.80%           
==========================================
  Files              ?      330           
  Lines              ?    41901           
  Branches           ?     6422           
==========================================
  Hits               ?    27572           
  Misses             ?    12262           
  Partials           ?     2067           
Flag Coverage Δ
GPU 65.80% <89.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Dec 02 '25 13:12 codecov-commenter

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

:white_check_mark: qwes5s5
:x: root


root seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Dec 24 '25 06:12 CLAassistant