Skip to content

Initialize lines = [] in load_log_files to avoid UnboundLocalError on FileNotFoundError retry failures#3879

Closed
Chessing234 wants to merge 1 commit into
lm-sys:mainfrom
Chessing234:fix/load-log-files-unbound-local-v2
Closed

Initialize lines = [] in load_log_files to avoid UnboundLocalError on FileNotFoundError retry failures#3879
Chessing234 wants to merge 1 commit into
lm-sys:mainfrom
Chessing234:fix/load-log-files-unbound-local-v2

Conversation

@Chessing234
Copy link
Copy Markdown

Bug:
In fastchat/serve/monitor/basic_stats.py, when all 5 retries to read a log file fail due to a FileNotFoundError, an UnboundLocalError: local variable 'lines' referenced before assignment is raised at for l in lines:.

Root Cause:
lines is only assigned a value when open(filename).readlines() executes successfully inside the try block. If a FileNotFoundError occurs for all 5 attempts, the loop terminates without lines being initialized, but the code still proceeds to access lines.

Why Fix is Correct:
By initializing lines = [] before starting the retry loop, we ensure that lines is always defined. If all retries fail, it remains an empty list, and the function gracefully skips the parsing loop and returns an empty list rather than crashing.

@Chessing234 Chessing234 closed this by deleting the head repository May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant