fix: resolve file handle leak and bare except in ood_upload_testcase - #15918
Closed
lxcxjxhx wants to merge 1 commit into
Closed
fix: resolve file handle leak and bare except in ood_upload_testcase#15918lxcxjxhx wants to merge 1 commit into
lxcxjxhx wants to merge 1 commit into
Conversation
- Use context manager (with statement) to properly close file handle - Replace bare except with specific requests.RequestException - Prevents resource leaks and improves error handling clarity This fix addresses two issues in the upload_testcase function: 1. File handle leak: open() was called without proper cleanup 2. Bare except clause: violated PEP8 E722 and could hide critical errors
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
I have read the CLA Document and I hereby sign the CLA |
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.
Description
Fixes two bugs in
infra/build/functions/ood_upload_testcase.py:open()called without context managerexcept:clause (PEP8 E722) that could causeNameErrorwhen request failsChanges
infra/build/functions/ood_upload_testcase.py(+8/-10 lines)with open()context manager for proper file handle cleanupexcept:toexcept requests.RequestException as eMotivation
The original code had two related bugs:
File handle leak: The file was opened but never explicitly closed, relying on garbage collection. This could lead to resource exhaustion when uploading many testcases.
Bare except with NameError risk: The bare
except:clause referencedein the error message, but bare except doesn't bind the exception to a variable. This would cause aNameErrorwhen trying to print the error.Testing
Apology
I sincerely apologize for my previous low-quality PRs to this repository. I have learned from those mistakes and am now focusing on single-issue, minimal, well-tested contributions. I hope this PR demonstrates my commitment to improving.