Skip to content

[BugFix] Add lock to avoid generating nan when using storage cache#7046

Open
juncaipeng wants to merge 2 commits intoPaddlePaddle:developfrom
juncaipeng:add_lock
Open

[BugFix] Add lock to avoid generating nan when using storage cache#7046
juncaipeng wants to merge 2 commits intoPaddlePaddle:developfrom
juncaipeng:add_lock

Conversation

@juncaipeng
Copy link
Copy Markdown
Collaborator

Motivation

Add lock to avoid generating nan when using storage cache

💡 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)

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

Modifications

fastdeploy/cache_manager/prefix_cache_manager.py

Usage or Command

none

Accuracy Tests

none

Checklist

  • 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.
  • Format your code, run pre-commit before commit.
  • Add unit tests. Please write the reason in this PR if no unit tests.
  • Provide accuracy results.
  • 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.

Copilot AI review requested due to automatic review settings March 27, 2026 07:42
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Mar 27, 2026

Thanks for your contribution!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

该 PR 旨在通过对 KVCache storage cache 的读/写回任务加锁,避免在特定并发访问场景下出现 NaN(通过 FD_USE_KVCACHE_LOCK 控制启用)。

Changes:

  • issue_write_back_storage_task 中对写回 storage 的流程加锁,并限制当前仅支持同步模式。
  • issue_prefetch_storage_task 中对从 storage 预取到 GPU 的流程加锁,并限制当前仅支持同步模式。
Comments suppressed due to low confidence (1)

fastdeploy/cache_manager/prefix_cache_manager.py:1260

  • 这里在 acquire lock 之后如果触发后续异常(例如 keys/gpu_block_ids 长度不一致时 raise ValueError,或 put_transfer_task 抛异常),会导致锁永远不释放,后续 worker 可能被永久阻塞。建议把参数校验放到加锁之前,并用 try/finally(或 contextmanager)保证 _release_kvcache_lock() 一定执行。
        assert is_sync, "Only support is_sync=True for now."
        self._acquire_kvcache_lock()

        if len(task.keys) != len(task.gpu_block_ids):
            err_msg = (
                f"write_back_storage error: hash_keys({len(task.keys)}) != gpu_block_ids({len(task.gpu_block_ids)})"
            )
            logger.error(err_msg)
            raise ValueError(err_msg)

Comment on lines +1252 to +1254
assert is_sync, "Only support is_sync=True for now."
self._acquire_kvcache_lock()

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

这里用 assert 来限制 is_sync=True:1) Python -O 会跳过 assert,约束不生效;2) 现有单测 tests/cache_manager/test_prefix_cache_manager.py 覆盖了 is_sync=False 的分支并期望抛 ValueError(key/gpu_block_ids 不匹配),这次改动会直接变成 AssertionError 导致用例失败。建议改为显式的运行时检查并抛出 NotImplementedError/ValueError,或保留对 is_sync=False 的兼容实现并同步更新单测。

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.18182% with 7 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@25d64ef). Learn more about missing BASE report.

Files with missing lines Patch % Lines
fastdeploy/cache_manager/prefix_cache_manager.py 71.42% 3 Missing and 3 partials ⚠️
.../transfer_factory/mooncake_store/mooncake_store.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #7046   +/-   ##
==========================================
  Coverage           ?   72.99%           
==========================================
  Files              ?      399           
  Lines              ?    56419           
  Branches           ?     8919           
==========================================
  Hits               ?    41183           
  Misses             ?    12309           
  Partials           ?     2927           
Flag Coverage Δ
GPU 72.99% <68.18%> (?)

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

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