LightRAG icon indicating copy to clipboard operation
LightRAG copied to clipboard

Fix: Remove redundant exception logging to eliminate pytest shutdown errors

Open danielaskdd opened this issue 1 week ago â€ĸ 1 comments

🐛 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:

  1. Deleted log_trace_exception function from lightrag/utils.py
  2. Removed all imports and usages from:
    • lightrag/api/routers/query_routes.py (3 occurrences)
    • lightrag/api/routers/ollama_api.py (4 occurrences)
  3. Cleaned up redundant exception handling:
    • Removed empty except Exception as e: raise blocks that served no purpose
  4. Preserved defensive safeguards:
    • Kept SafeStreamHandler for graceful stream closure handling
    • Kept _patch_ascii_colors_console_handler() monkey-patch as a defensive measure

Why This Works

  • FastAPI automatically logs exceptions with full stack traces - no additional logging layer needed
  • Standard Python logging with SafeStreamHandler and 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.

danielaskdd avatar Nov 19 '25 13:11 danielaskdd

@codex review

danielaskdd avatar Nov 19 '25 13:11 danielaskdd

@codex review

danielaskdd avatar Nov 19 '25 14:11 danielaskdd

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".