Fix load_log_files UnboundLocalError when all FileNotFoundError retries fail#3858
Open
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Open
Fix load_log_files UnboundLocalError when all FileNotFoundError retries fail#3858Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Chessing234 wants to merge 1 commit intolm-sys:mainfrom
Conversation
…ries 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
fastchat/serve/monitor/basic_stats.py::load_log_filesopens the log file inside a 5-attempt retry loop and then iterateslines:If the file never becomes available (wrong path, producer hasn't written it yet, stale watch), every iteration hits the
exceptbranch. The loop completes without executinglines = ..., so the followingfor l in lines:raisesUnboundLocalError: local variable 'lines' referenced before assignment. That error buries the real cause —FileNotFoundError— and points at a line where nothing is actually wrong.load_log_files_parallelcalls this through aPool.imap, so on a missing shard the traceback surfaced to the caller isUnboundLocalErrorwith no filename, instead of a clear "this file is missing."Root cause
The retry loop swallows
FileNotFoundErrorbut has no terminal branch for "all retries exhausted." Python doesn't care that the symbol is annotated in thetry; the binding only happens on a successfulopen().Why the fix is correct
Attach a
for/elsewhoseelseonly runs when the loop wasn't broken out of — i.e. when every retry failed — and re-raise the real exception with the filename:for retry in range(5): try: lines = open(filename).readlines() break except FileNotFoundError: time.sleep(2) + else: + raise FileNotFoundError(f"Failed to open {filename} after 5 retries") for l in lines:breakskips theelse, behaviour is unchanged.UnboundLocalErroris replaced byFileNotFoundErrornaming the actual file, matching the exception that the loop is explicitly retrying.Two added lines; no other changes in this function or file.