From 2743083447661cacba398b76f1271a1ee5a980c3 Mon Sep 17 00:00:00 2001 From: Taksh Date: Mon, 20 Apr 2026 11:02:26 +0530 Subject: [PATCH] Fix load_log_files UnboundLocalError when all 5 FileNotFoundError retries fail load_log_files opens the log file inside a retry loop: for retry in range(5): try: lines = open(filename).readlines() break except FileNotFoundError: time.sleep(2) for l in lines: ... If the file never becomes available (e.g. the caller passed a bad path, or the producer never writes it), every iteration hits the except branch, the loop completes without binding lines, and the next statement raises 'UnboundLocalError: local variable lines referenced before assignment' -- obscuring the real cause (FileNotFoundError) with a confusing local-scope error that points nowhere useful. Adding a for/else that raises FileNotFoundError after the retries run out turns this into a correct, explicit failure naming the actual file. The break in the happy path keeps the else branch unreached on success, so there is no behaviour change when the file opens within 5 tries. --- fastchat/serve/monitor/basic_stats.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fastchat/serve/monitor/basic_stats.py b/fastchat/serve/monitor/basic_stats.py index 3c1a8793d..87724177e 100644 --- a/fastchat/serve/monitor/basic_stats.py +++ b/fastchat/serve/monitor/basic_stats.py @@ -42,6 +42,8 @@ def load_log_files(filename): break except FileNotFoundError: time.sleep(2) + else: + raise FileNotFoundError(f"Failed to open {filename} after 5 retries") for l in lines: row = json.loads(l)