fix: prevent cross-request contamination in CitationRegistry (#394)#408
Open
MestreY0d4-Uninter wants to merge 1 commit intoOpenBMB:mainfrom
Open
fix: prevent cross-request contamination in CitationRegistry (#394)#408MestreY0d4-Uninter wants to merge 1 commit intoOpenBMB:mainfrom
MestreY0d4-Uninter wants to merge 1 commit intoOpenBMB:mainfrom
Conversation
…ead-local storage - Replace class-level _instances dict with thread-local storage - CitationRegistry and SurveyCPMCitationRegistry now isolate state per thread - Fixes race condition where concurrent requests could corrupt citation IDs - Add test case demonstrating thread isolation Fixes: OpenBMB#394
Author
Test CoverageAdded python3 test_citation_registry_fix.pyResults:
The fix uses Python's |
Author
Evidencia Tecnica do FixProblema OriginalO codigo original usava estado de classe compartilhado: class CitationRegistry:
_instances: Dict[int, Dict[str, Any]] = {} # ❌ Compartilhado globalmente
@classmethod
def reset(cls):
cls._instances = {} # ❌ Limpa tudo para TODAS as threadsSolucao ImplementadaThread-local storage isola estado por thread de execucao: _citation_registry_local = threading.local()
class CitationRegistry:
@classmethod
def reset(cls):
if not hasattr(_citation_registry_local, '_instances'):
_citation_registry_local._instances = {}
else:
_citation_registry_local._instances.clear()Validacao
Impacto
|
Author
Melhorias no Hub de Contribuição (Global)Além do fix da issue #394, este PR também inclui melhorias globais no hub de contribuição: Problemas Corrigidos
Soluções Implementadas
Impacto
Estas melhorias são globais e beneficiam todo o ecossistema UltraRAG, não apenas o fix específico da #394. |
a101fe5 to
893ef6c
Compare
Author
UpdateSquashed to single commit with only the fix for issue #394. Changes:
All other changes were unrelated to this PR and have been removed. |
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.
Description
This PR fixes a critical race condition bug in CitationRegistry where concurrent requests could corrupt citation IDs due to shared class-level state.
Problem
The original implementation used a class-level dictionary (
_instances) shared across all requests. When two concurrent requests calledinit_citation_registry→assign_citation_ids_stateful, one request'sreset()would wipe the global state while the other was mid-execution, causing:Solution
Replace class-level storage with thread-local storage using Python's
threading.local(). This ensures:Changes
servers/custom/src/custom.py: RefactorCitationRegistryandSurveyCPMCitationRegistryto use thread-local storagetest_citation_registry_fix.py: Add test demonstrating thread isolationTesting
Test passes with concurrent thread execution showing isolated state:
Related Issue
Fixes #394