fix: Never call simple_search from exception handler during context cleanup

This commit is contained in:
2026-01-01 15:19:33 +01:00
parent 4492814891
commit 22ac9a030e

View File

@@ -481,8 +481,7 @@ def hierarchical_search(
except ValueError as e: except ValueError as e:
# Check if this is our fallback signal # Check if this is our fallback signal
error_msg = str(e) if str(e) == "FALLBACK_TO_SIMPLE":
if error_msg == "FALLBACK_TO_SIMPLE" or error_msg.startswith("FALLBACK_ERROR:"):
# Fallback to simple search (outside context manager) # Fallback to simple search (outside context manager)
results = simple_search(query, limit, author_filter, work_filter) results = simple_search(query, limit, author_filter, work_filter)
return { return {
@@ -497,20 +496,15 @@ def hierarchical_search(
import traceback import traceback
traceback.print_exc() traceback.print_exc()
# Fallback to simple search on error (unless forced) # CRITICAL: Never call simple_search() here - we're still cleaning up the context manager!
if not force_hierarchical: # Instead, return empty result and let the caller decide if they want to retry
# CRITICAL: We're still inside the 'with' block here! return {
# Signal fallback to exit context manager before calling simple_search() "mode": "hierarchical" if force_hierarchical else "simple",
raise ValueError(f"FALLBACK_ERROR: {str(e)}") "sections": [],
else: "results": [],
# Forced hierarchical: return error in hierarchical format "total_chunks": 0,
return { "fallback_reason": f"Erreur lors de la recherche: {str(e)}",
"mode": "hierarchical", }
"sections": [],
"results": [],
"total_chunks": 0,
"fallback_reason": f"Erreur lors de la recherche hiérarchique: {str(e)}",
}
def should_use_hierarchical_search(query: str) -> bool: def should_use_hierarchical_search(query: str) -> bool: