Fix error handling bugs in url.py makedirs and download_url#2207
Open
Mr-Neutr0n wants to merge 1 commit intoRUCAIBox:masterfrom
Open
Fix error handling bugs in url.py makedirs and download_url#2207Mr-Neutr0n wants to merge 1 commit intoRUCAIBox:masterfrom
Mr-Neutr0n wants to merge 1 commit intoRUCAIBox:masterfrom
Conversation
- Fix makedirs(): the except clause had an inverted condition (e.errno != errno.EEXIST and osp.isdir(path)) that silently swallowed real OS errors like PermissionError when the path did not exist. Replace with os.makedirs(exist_ok=True) which is the correct Python 3 approach and handles all edge cases properly. - Fix download_url(): replace bare except clause with except Exception to allow KeyboardInterrupt and SystemExit to propagate correctly. Re-raise the original exception instead of replacing it with a generic RuntimeError to preserve the traceback for debugging. - Fix resource leak: ensure the URL connection opened by ur.urlopen() is always closed via try/finally, preventing socket/file descriptor leaks on both success and failure paths.
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.
Summary
Fixes three related error handling bugs in
recbole/utils/url.py:1.
makedirs()silently swallows real OS errorsThe except clause has an inverted condition:
This means when
errno != EEXIST(e.g.,PermissionError) and the path doesn't exist as a directory, the error is silently swallowed. The function returns successfully but the directory was never created, causing confusing failures downstream.Fix: Replace with
os.makedirs(exist_ok=True), which is the correct Python 3 approach and handles all edge cases properly.2.
download_url()bareexcept:catches KeyboardInterruptThe bare
except:clause catchesKeyboardInterruptandSystemExit, making it difficult to interrupt a long download with Ctrl+C — the user gets a genericRuntimeError("Stopped downloading due to interruption.")instead of a clean exit, and the original traceback is lost.Fix: Use
except Exception:and re-raise the original exception withraiseto preserve the traceback.3.
download_url()leaks the URL connectionThe
ur.urlopen(url)response is never closed, leaking socket/file descriptors. This matters whendownload_urlis called repeatedly or when an exception occurs mid-download.Fix: Wrap the download logic in
try/finallyto ensuredata.close()is always called.Test plan
makedirsworks for new directoriesmakedirsis a no-op for existing directoriesmakedirsraises on permission errors instead of silently failingKeyboardInterruptcleanly