Skip to content

[https://nvbugs/6964706][fix] Shut down the KV cache managers the forward-pass-callable tests create - #20816

Closed
trtllm-agent wants to merge 1 commit into
NVIDIA:mainfrom
tensorrt-cicd:repair-bot-v2/nvbug-6964706-r44
Closed

trtllm-agent wants to merge 1 commit into
NVIDIA:mainfrom
tensorrt-cicd:repair-bot-v2/nvbug-6964706-r44

Conversation

@trtllm-agent

@trtllm-agent trtllm-agent commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Five tests in tests/unittest/_torch/executor/test_pytorch_model_engine.py create a KVCacheManagerV2 and never shut it down. This change registers kv_cache_manager.shutdown as a test cleanup in each of them, as the other tests in the file already do.

Fixes https://nvbugs/6964706

Root cause

  • test_forward_pass_callable_on_cuda_graph_on, test_forward_pass_callable_on_cuda_graph_off, test_foward_pass_callable_off, test_foward_pass_callable_backward_compat and test_prepare_inputs_with_helix_parallelism leave their manager to be released by garbage collection.
  • When a reference cycle keeps the manager alive past the test, the cyclic GC frees it on whichever thread runs the collection. On a thread with no current CUDA context, the KV cache teardown (CachedCudaEvent -> cuEventRecord) fails with CUDA driver error: 201 (invalid device context). That sets the process-wide KVCM2 poison latch.
  • Every later KVCM2 operation in the same pytest process then fails with CorruptedError: KV cache manager is unusable. The filed test, test_pad_batch_strips_cudagraph_dummies_on_clean_exit, is the first such victim; about 20 further executor tests fail the same way.
  • In the failing CI runs, the first victim always follows these four forward-pass-callable tests. This is the same mechanism that [None][perf] Fill the KVCM2 host tier progressively #19650 fixed for test_pytorch_model_engine_warmup.py and test_dsa_indexer.
  • The poison also causes the later HostOOMError: mmap failed: Cannot allocate memory failures in test_pytorch_model_engine_warmup.py. Once KVCM2 is poisoned, HostMemBacking::destroy() deliberately keeps its host range mapped instead of unmapping it, so each manager created afterwards leaks its host tier until mmap fails. In the CI run that filed this bug, the HostOOM failure (test_helix_cp_runs_only_the_generation_autotuner_warmup) comes after the first CorruptedError victim.

Change

self.addCleanup(kv_cache_manager.shutdown) right after each of the five managers is created. Test-only; no assertion, skip or test-list change.

Verification (B300, main 9bb7931, CI image, wheel built from that commit)

unittest/_torch/executor run in one pytest process, as the CI stage runs it, with a diagnostic plugin (not part of this change). The plugin runs gc.collect() on a fresh thread without a CUDA context at every test boundary, records the creating test and finalizing thread of each manager, and reports the poison state after each test.

Without this change With this change
test_pad_batch_strips_cudagraph_dummies_on_clean_exit FAILED (CorruptedError ... CUDA driver error: 201) PASSED
Tests failing with CorruptedError 21 (reproduced twice) 0
test_pytorch_model_engine_warmup.py cases failing with HostOOMError: mmap failed 5 (TestWarmupCleanup x3, test_warmup_phases_pass_configured_speculation_state[mamba], [prefill]) 0 (all pass)
KVCM2 poisoned yes, right after the test_foward_pass_callable_backward_compat manager is finalized by the GC on the context-less thread never

The other failures are the same with and without this change and are specific to the local environment, not this change: test_mamba_cache_manager.py (SimpleNamespace has no mapping) and test_overlap_scheduler.py (no model path); both pass in CI. Without the plugin, test_pytorch_model_engine.py and test_pytorch_model_engine_warmup.py together pass in full with this change (117 passed).

Dev Engineer Review

The change adds explicit cleanup for five KV cache managers. It calls shutdown during test cleanup instead of relying on garbage collection. This targets CUDA-context teardown failures that could poison KVCM2 for later tests. No review findings or independent test results were supplied.

QA Engineer Review

The modified test file adds cleanup registration to five tests. The changes cover KV cache manager teardown and the reported CUDA error 201 failure path. The author reports that the affected test passed, KVCM2 was not poisoned, and two named test files completed with 117 passing tests without the diagnostic plugin. Coverage verdict: needs follow-up; the reported results do not establish broader test coverage.

Per-File QA Perspective

  • tests/unittest/_torch/executor/test_pytorch_model_engine.py: Five tests register kv_cache_manager.shutdown as test cleanup. Verify cleanup runs reliably and prevents CUDA-context teardown failures. The file is a unit test and does not require listing in the integration test lists.

…ward-pass-callable tests create

test_forward_pass_callable_on_cuda_graph_{on,off}, test_foward_pass_callable_{off,backward_compat}
and test_prepare_inputs_with_helix_parallelism created a KVCacheManagerV2 and never shut it
down. A manager kept alive by a reference cycle is then freed by the cyclic GC on whichever
thread runs the collection. On a thread without a current CUDA context the KV cache teardown
fails with CUDA error 201, which poisons KVCM2 for the rest of the process, so every later V2
test in the same pytest process fails with CorruptedError.

Register kv_cache_manager.shutdown as a test cleanup, as the other tests in the file do.

Signed-off-by: trtllm-agent <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
📥 Commits

Reviewing files that changed from the base of the PR and between d4cfc12 and 425c9a5.

📒 Files selected for processing: 1
  • tests/unittest/_torch/executor/test_pytorch_model_engine.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

Walkthrough

Five tests in the PyTorch model engine test suite register the KV cache manager’s shutdown method to run during test cleanup.

Changes

KV Cache Manager Test Cleanup

Layer / File(s) Summary
Register test cleanup
tests/unittest/_torch/executor/test_pytorch_model_engine.py
Five tests register the KV cache manager’s shutdown method as a test cleanup action.

Priority: ➖ Normal

Estimated code review effort: 1 (Trivial) | ~4 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 425c9

The cleanup change is narrowly scoped and introduces no identified merge-blocking risk.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the NVBugs issue, fix type, and primary change: explicit shutdown of KV cache managers in forward-pass-callable tests.
Description check Passed The description clearly explains the issue, root cause, change, affected tests, and verification results. It does not use the template headings exactly and does not include a completed checklist, but …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description clearly explains the issue, root cause, change, affected tests, and verification results. It does not use the template headings exactly and does not include a completed checklist, but it contains the required substantive information.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@mikeiovine mikeiovine left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Stamp on behalf of runtime devs

@github-actions

Copy link
Copy Markdown

Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements.

@BowenFu

BowenFu commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

/bot run

@github-actions

Copy link
Copy Markdown

👎 Promotion blocked, new vulnerability found

Vulnerability report

Component Vulnerability Description Severity
py-filelock CVE-2026-112906 filelock is a platform-independent file lock for Python. From 4.1.0 until 4.2.1, SoftFileLock versions from 4.1.0 can reuse a stale observation when an attacker pre-dates and removes its own marker, at least two lock-user processes are waiting, coarse or matching modification times preserve the observation, and an honest contender creates a fresh marker on the reused inode, causing that live marker to be removed as stale. This issue is fixed in version 4.2.1. MEDIUM
py-filelock CVE-2026-112914 filelock is a platform-independent file lock for Python. From 4.1.0 until 4.2.1, SoftFileLock can restore a stale marker by name after moving it aside, allowing another local user with rename access in a non-sticky lock directory to swap in a symbolic link and, on a runtime that follows the link and a same-filesystem target, make the lock user hard-link one of its files into the lock directory. This issue is fixed in version 4.2.1. MEDIUM
py-filelock CVE-2026-112937 filelock is a platform-independent file lock for Python. From 4.1.0 until 4.2.1, ReadWriteLock can remove a randomized .filelock-* private-hard-link directory during cleanup, allowing another local user with rename access in a non-sticky parent to observe a live name or forge a dead-owner sweep name and rename a sibling directory onto it for deletion. This issue is fixed in version 4.2.1. MEDIUM
py-filelock CVE-2026-112940 filelock is a platform-independent file lock for Python. From 3.20.4 until 4.2.1, ReadWriteLock let SQLite reopen the database and side files by name; on POSIX systems an attacker with rename access in a non-sticky parent can swap those names to cause overwrite or unlink operations, while a writable adjacent hot journal is additionally required for attacker-chosen content overwrites. This issue is fixed in version 4.2.1. HIGH

@BowenFu

BowenFu commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80832 [ run ] triggered by Bot. Commit: 425c9a5 Link to invocation

@BowenFu
BowenFu enabled auto-merge (squash) October 27, 2026 08:42
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #80832 [ run ] completed with state SUCCESS. Commit: 425c9a5
/LLM/main/L0_MergeRequest_PR pipeline #66561 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

CI Agent Failure Analysis

Link to invocation

@BowenFu

BowenFu commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded by #21158 (merged 10/29). That refactor removed tests/unittest/_torch/executor/test_pytorch_model_engine.py and moved these tests onto context-managed helpers in engine_test_utils.py (create_model_engine_and_kvcache / kv_cache_manager), which call manager.shutdown() on exit. The leaked KVCacheManagerV2 this PR fixes can no longer occur. nvbugs/6964706 should be re-verified against current main post-merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants