fix: replace 4 bare except clauses with except Exception#61307
fix: replace 4 bare except clauses with except Exception#61307haosenwang1018 wants to merge 1 commit intoray-project:masterfrom
Conversation
Bare `except:` catches BaseException including KeyboardInterrupt and SystemExit. Replaced 4 instances with `except Exception:`.
There was a problem hiding this comment.
Code Review
This pull request correctly replaces several bare except: clauses with except Exception:, which is a good practice to avoid catching system-level exceptions.
I've also reviewed the other changes in this PR, which seem to involve vendoring some test utility scripts. I found a few issues in these new files:
- The
test_myst_doc.pyscripts create temporary files that are not cleaned up, leading to resource leaks. test_v1_persistence.pyhas an incorrect type hint for a method's return value.test_v2_persistence.pyhas a bug that would cause aNameErrorduring test execution.
I've added specific comments with suggestions for these issues. Please take a look.
I am having trouble creating individual review comments. Click here to see my feedback.
release/train_tests/multinode_persistence/test_v2_persistence.py (228-234)
The variable test_trainer is used on line 266 but is not defined in the function signature, which will cause a NameError. It should be added as a parameter to the function. The call site in test_trainer will also need to be updated.
def _assert_storage_contents(
local_inspect_dir: Path,
exp_name: str,
checkpoint_config: CheckpointConfig,
test_trainer: bool,
no_checkpoint_ranks: List[int] = None,
constants: type = TestConstants,
):
release/train_tests/multinode_persistence/test_v2_persistence.py (376-381)
After adding test_trainer to the _assert_storage_contents function signature, you need to pass its value here. Since this function is test_trainer, test_trainer=True should be passed.
_assert_storage_contents(
local_inspect_dir,
exp_name,
checkpoint_config,
test_trainer=True,
no_checkpoint_ranks=no_checkpoint_ranks,
)
release/air_examples/dolly_v2_lightning_fsdp_finetuning/test_myst_doc.py (72-84)
The temporary file created with tempfile.NamedTemporaryFile("w", delete=False) is not deleted after use. This will lead to an accumulation of temporary files in the system's temporary directory. It's best practice to ensure these files are cleaned up, for example by using a try...finally block.
name = ""
try:
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") as f:
# Define the display function, which is available in notebooks,
# but not in normal Python scripts.
f.write(DISPLAY_FUNCTION)
jupytext.write(notebook, f, fmt="py:percent")
name = f.name
remainder.insert(0, name)
remainder.insert(0, sys.executable)
# Run the notebook
subprocess.run(remainder, check=True)
finally:
if name:
Path(name).unlink(missing_ok=True)
release/air_examples/opt_deepspeed_batch_inference/test_myst_doc.py (72-84)
The temporary file created with tempfile.NamedTemporaryFile("w", delete=False) is not deleted after use. This will lead to an accumulation of temporary files in the system's temporary directory. It's best practice to ensure these files are cleaned up, for example by using a try...finally block.
name = ""
try:
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") as f:
# Define the display function, which is available in notebooks,
# but not in normal Python scripts.
f.write(DISPLAY_FUNCTION)
jupytext.write(notebook, f, fmt="py:percent")
name = f.name
remainder.insert(0, name)
remainder.insert(0, sys.executable)
# Run the notebook
subprocess.run(remainder, check=True)
finally:
if name:
Path(name).unlink(missing_ok=True)
release/air_examples/vicuna_13b_lightning_deepspeed_finetuning/test_myst_doc.py (72-84)
The temporary file created with tempfile.NamedTemporaryFile("w", delete=False) is not deleted after use. This will lead to an accumulation of temporary files in the system's temporary directory. It's best practice to ensure these files are cleaned up, for example by using a try...finally block.
name = ""
try:
with tempfile.NamedTemporaryFile("w", delete=False, suffix=".py") as f:
# Define the display function, which is available in notebooks,
# but not in normal Python scripts.
f.write(DISPLAY_FUNCTION)
jupytext.write(notebook, f, fmt="py:percent")
name = f.name
remainder.insert(0, name)
remainder.insert(0, sys.executable)
# Run the notebook
subprocess.run(remainder, check=True)
finally:
if name:
Path(name).unlink(missing_ok=True)
release/train_tests/multinode_persistence/test_v1_persistence.py (261)
The type hint for the return value is str, but the function can also return a dict on line 263. The type hint should be updated to typing.Union[str, dict] to reflect the actual return types. You may need to add import typing or from typing import Union.
def save_checkpoint(self, temp_checkpoint_dir) -> "typing.Union[str, dict]":
What
Replace 4 bare
except:clauses withexcept Exception:.Why
Bare
except:catchesBaseException, includingKeyboardInterruptandSystemExit, which can prevent clean process shutdown and mask critical errors. Usingexcept Exception:catches all application-level errors while allowing system-level exceptions to propagate correctly.