Skip to content

[https://nvbugs/5961736][fix] Prebuild disagg ctx response to avoid ctx_request_id race#12466

Merged
pcastonguay merged 5 commits into
NVIDIA:mainfrom
peihu-nv:fix/nvbug-5961736-disagg-ctx-eager-response
Apr 8, 2026
Merged

[https://nvbugs/5961736][fix] Prebuild disagg ctx response to avoid ctx_request_id race#12466
pcastonguay merged 5 commits into
NVIDIA:mainfrom
peihu-nv:fix/nvbug-5961736-disagg-ctx-eager-response

Conversation

@peihu-nv

@peihu-nv peihu-nv commented Mar 23, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Fixed duplicate termination behavior in request handling for context-only scenarios, preventing resource conflicts.
  • Performance

    • Optimized response creation timing to reduce latency in asynchronous request processing.

Description

Fixes a race in disaggregated serving where the CTX response can reach the orchestrator with ctx_request_id=None, causing a 500 error.

Root cause: _check_disagg_ctx_cache_transfer_status(0) in _send_kv_async() can advance a ctx-only request from DISAGG_CONTEXT_TRANS_IN_PROGRESS to DISAGG_CONTEXT_COMPLETE before _handle_responses() runs. Once that happens, C++ createResult() returns nullopt for contextPhaseParams, so the CTX response no longer carries ctx_request_id.

Fix: call create_response() immediately after respond_and_send_async() while the request is still guaranteed to be in the transfer-in-progress state, store the result as _prebuilt_ctx_response, and consume it later in _handle_responses() without calling create_response() again.

Test Coverage

N/A

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

…capture

The ctx response (containing ctx_request_id and opaque KV state) was only
accessible while the request was in DISAGG_CONTEXT_TRANS_IN_PROGRESS.
_check_disagg_ctx_cache_transfer_status(0) could advance the state to
DISAGG_CONTEXT_COMPLETE before _handle_responses ran, causing C++
createResult() to return nullopt and leaving ctx_request_id=None.

Fix: eagerly call create_response() immediately after respond_and_send_async()
in _send_kv_async, while TRANS_IN_PROGRESS is guaranteed. The response is
latched on the request (_eager_ctx_response) and consumed directly by
_handle_responses, which skips create_response() for those requests.

Safety of calling releaseState() here: CacheFormatter::format() (the CTX-side
KV sender) reads only mRequestId, mSamplingConfig, getContextProgress(), and
KV block data — it does NOT read mContextPhaseParams. The opaque state is
only needed by the GEN-side unformat(), which runs in a separate process.

Lifecycle is preserved: _handle_responses removes the request from
active_requests (does not re-add to new_active_requests) without calling
_terminate_request, leaving termination to _check_disagg_ctx_cache_transfer_status
in both the in-flight (Case A) and already-complete (Case B) cases.

Signed-off-by: peihengh <259410613+peihu-nv@users.noreply.github.com>
@peihu-nv
peihu-nv requested a review from a team as a code owner March 23, 2026 23:13
@peihu-nv
peihu-nv requested a review from achartier March 23, 2026 23:13
@coderabbitai

coderabbitai Bot commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: baacd9fe-cd94-49aa-b43e-adcc68bb291f

📥 Commits

Reviewing files that changed from the base of the PR and between 90c1cb7 and 1ce2186.

📒 Files selected for processing (1)
  • tensorrt_llm/_torch/pyexecutor/py_executor.py

📝 Walkthrough

Walkthrough

Modified response handling in the PyExecutor to capture and store context-only responses upfront during asynchronous KV transfer, then reuse the prebuilt response during response processing while adjusting termination logic to prevent duplicate request termination.

Changes

Cohort / File(s) Summary
Response Lifecycle Management
tensorrt_llm/_torch/pyexecutor/py_executor.py
Modified _send_kv_async to capture and cache the response object in req._prebuilt_ctx_response immediately after starting async KV transfer. Updated _handle_responses to conditionally use prebuilt responses when available, bypassing standard response creation and properly merging cached tokens. Adjusted termination logic to prevent re-adding requests that already consumed prebuilt responses, eliminating duplicate termination scenarios.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: prebuilding the disagg ctx response to fix a race condition involving ctx_request_id, matching the core fix described in the summary.
Description check ✅ Passed The description includes a clear explanation of the issue, root cause, and solution, plus the completed PR checklist. However, 'Test Coverage' section states 'N/A' without justifying why no tests are provided.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@peihu-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

1 similar comment
@peihu-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40146 [ run ] triggered by Bot. Commit: 1ce2186 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40146 [ run ] completed with state FAILURE. Commit: 1ce2186
/LLM/main/L0_MergeRequest_PR pipeline #31292 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@peihu-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40156 [ run ] triggered by Bot. Commit: fdb2bdb Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40156 [ run ] completed with state SUCCESS. Commit: fdb2bdb
/LLM/main/L0_MergeRequest_PR pipeline #31302 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@peihu-nv

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40168 [ run ] triggered by Bot. Commit: fdb2bdb Link to invocation

Comment thread tensorrt_llm/_torch/pyexecutor/py_executor.py
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #40168 [ run ] completed with state SUCCESS. Commit: fdb2bdb
/LLM/main/L0_MergeRequest_PR pipeline #31313 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

Comment thread tensorrt_llm/_torch/pyexecutor/py_executor.py Outdated
Comment thread tensorrt_llm/_torch/pyexecutor/py_executor.py Outdated
…eateResult fix

When the KV transfer poll in _send_kv_async advanced request state to
DISAGG_CONTEXT_COMPLETE before _handle_responses ran, createResult()
returned nullopt (its disagg-ctx branch only fired for TRANS_IN_PROGRESS),
causing ctx_request_id=None and the error "Invalid disaggregated params
in context phase response."

Fix:
- isFinished(): extend to cover DISAGG_CONTEXT_COMPLETE so createResult()
  passes the early-return guard for that state.
- createResult(): broaden the disagg-ctx branch condition from
  isDisaggContextTransmissionState() to also match isDisaggContextCompleteState(),
  so the context phase params are included in the response regardless of
  which state the race resolves to. mContextPhaseParams is still valid at
  this point since releaseState() is called by createResult() itself.
- py_executor.py _handle_responses(): guard the requests_to_terminate
  logic to skip DISAGG_CONTEXT_COMPLETE requests (already terminated by
  _check_disagg_ctx_cache_transfer_status), preventing double-free.
  Track them in requests_finished_by_transfer for stats visibility.

Signed-off-by: peihengh <259410613+peihu-nv@users.noreply.github.com>
…lt with DISAGG_CONTEXT_COMPLETE

Adds a unit test that constructs a context-only request in
kDISAGG_CONTEXT_COMPLETE state and verifies createResult() returns a
valid response with contextPhaseParams populated. Uses nullptr for the
opaque state — sufficient to prove the fix (the bug was createResult()
returning nullopt, not state pointer corruption). A follow-up could
strengthen this with a non-null sentinel state and firstGenTokens check.

Signed-off-by: peihengh <259410613+peihu-nv@users.noreply.github.com>
@peihu-nv

Copy link
Copy Markdown
Collaborator Author

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41027 [ run ] triggered by Bot. Commit: a311df9 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41027 [ run ] completed with state FAILURE. Commit: a311df9
/LLM/main/L0_MergeRequest_PR pipeline #32007 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@peihu-nv

peihu-nv commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41232 [ run ] triggered by Bot. Commit: 957218f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41232 [ run ] completed with state FAILURE. Commit: 957218f
/LLM/main/L0_MergeRequest_PR pipeline #32196 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@peihu-nv

peihu-nv commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run

@peihu-nv

peihu-nv commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41336 [ run ] triggered by Bot. Commit: 957218f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #41336 [ run ] completed with state FAILURE. Commit: 957218f

Link to invocation

@pcastonguay

Copy link
Copy Markdown
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #42171 [ run ] triggered by Bot. Commit: 957218f Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #42171 [ run ] completed with state SUCCESS. Commit: 957218f
/LLM/main/L0_MergeRequest_PR pipeline #32998 completed with status: 'SUCCESS'

CI Report

Link to invocation

@pcastonguay
pcastonguay merged commit 3bd37ae into NVIDIA:main Apr 8, 2026
5 checks passed
suyoggupta pushed a commit to nv-auto-deploy/TensorRT-LLM that referenced this pull request Apr 8, 2026
…tx_request_id race (NVIDIA#12466)

Signed-off-by: peihengh <259410613+peihu-nv@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants