LightRAG
LightRAG copied to clipboard
Fix: Remove redundant exception logging to eliminate pytest shutdown errors
đ Fix: Remove redundant exception logging to eliminate pytest shutdown errors
Problem
During pytest execution, a "Logging Error in ConsoleHandler" was occurring at shutdown:
--- Logging Error in ConsoleHandler ---
Error flushing stream during close
--- Traceback ---
Traceback (most recent call last):
File "/Users/ydh/mycode/ai/paper-RAG/.venv/lib/python3.12/site-packages/ascii_colors/__init__.py", line 998, in close
self.stream.flush()
ValueError: I/O operation on closed file.
--- End Logging Error ---
Root Cause: The log_trace_exception function attempted to use ascii_colors logging after pytest had already closed output streams during shutdown.
Solution
Removed the redundant log_trace_exception exception logging layer:
- Deleted
log_trace_exceptionfunction fromlightrag/utils.py - Removed all imports and usages from:
lightrag/api/routers/query_routes.py(3 occurrences)lightrag/api/routers/ollama_api.py(4 occurrences)
- Cleaned up redundant exception handling:
- Removed empty
except Exception as e: raiseblocks that served no purpose
- Removed empty
- Preserved defensive safeguards:
- Kept
SafeStreamHandlerfor graceful stream closure handling - Kept
_patch_ascii_colors_console_handler()monkey-patch as a defensive measure
- Kept
Why This Works
- FastAPI automatically logs exceptions with full stack traces - no additional logging layer needed
- Standard Python logging with
SafeStreamHandlerand ascii_colors patch provides robust error handling - Simpler code path reduces potential for shutdown-related race conditions
Impact
- â Cleaner, more maintainable code
- â Eliminates annoying shutdown error messages
- â Reduces logging complexity
- â Maintains full error visibility through FastAPI's built-in logging
- â No behavioral changes for end users
Backward Compatibility
â Fully backward compatible - This change only affects internal error logging; all external APIs and behaviors remain unchanged.
@codex review
@codex review
Codex Review: Didn't find any major issues. Another round soon, please!
âšī¸ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with đ.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".