fix: ensure single shared SyncPrefixHashTable instance across Store a…#2096
Merged
varungup90 merged 2 commits intovllm-project:mainfrom Apr 11, 2026
Merged
fix: ensure single shared SyncPrefixHashTable instance across Store a…#2096varungup90 merged 2 commits intovllm-project:mainfrom
varungup90 merged 2 commits intovllm-project:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request transitions the SyncPrefixHashTable to a singleton pattern to ensure consistent indexing across different components such as the Store and the gateway router. While the implementation correctly uses sync.Once for thread-safe initialization, the transition introduces a significant lifecycle risk: calling Close() on the shared instance (for example, during Store cleanup) will permanently stop the background eviction worker for all components without a way to restart it. Additionally, the new unit tests should be updated to close the existing instance before resetting the singleton state to prevent leaking background goroutines.
…nd kvSyncRouter Problem: Previously, Store and kvSyncPrefixCacheRouter created separate instances of SyncPrefixHashTable, leading to inconsistent prefix cache state and potential cache misses. Solution: Implement singleton pattern for SyncPrefixHashTable: - Add GetSharedSyncPrefixHashTable() function - Use sync.Once to ensure only one instance is created - Update Store (cache_init.go) to use shared instance - Update kvSyncRouter (prefix_cache.go) to use shared instance Lifecycle Management: - Store.cleanupKVEventSync() no longer closes the shared singleton - This prevents breaking other components (e.g., gateway router) that may still be using the shared instance - The singleton relies on process exit for cleanup (consistent with current behavior where Store.Close() is not explicitly called) Testing: - Added singleton_test.go with comprehensive tests - Fixed goroutine leak in concurrent test by closing old instance before resetting singleton state - Tests verify single instance creation - Tests verify thread-safe concurrent access - Tests verify functionality persistence across references Signed-off-by: penfree <penfree.qiu@gmail.com> Change-Id: I4261b75ca31f33465a51f7fd7aa6bba7c5435de1 Signed-off-by: penfree <penfree.qiu@gmail.com>
a4c700c to
5d6a8a0
Compare
varungup90
approved these changes
Apr 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Ensure Single Shared SyncPrefixHashTable Instance Across Components
Problem
Multiple components created separate instances of
SyncPrefixHashTable, leading to inconsistent prefix cache state:pkg/cache/cache_init.go:566) created its own instancepkg/plugins/gateway/algorithms/prefix_cache.go:303) created another instanceImpact:
Example Scenario
Root Cause
The
SyncPrefixHashTablewas designed to be a shared, centralized index. However, the code directly calledNewSyncPrefixHashTable()in multiple locations without ensuring a single shared instance.Solution
Implement singleton pattern to guarantee all components reference the same instance:
1. Add Singleton Function
File:
pkg/utils/syncprefixcacheindexer/sync_hash.goFeatures:
sync.OnceGetSharedPrefixHashTable2. Update All Call Sites
Testing
New Test Suite
Added
pkg/utils/syncprefixcacheindexer/singleton_test.gowith tests for: