Skip to content

fix: replace 4 bare except clauses with except Exception#61307

Open
haosenwang1018 wants to merge 1 commit intoray-project:masterfrom
haosenwang1018:fix/bare-excepts
Open

fix: replace 4 bare except clauses with except Exception#61307
haosenwang1018 wants to merge 1 commit intoray-project:masterfrom
haosenwang1018:fix/bare-excepts

Conversation

@haosenwang1018
Copy link

What

Replace 4 bare except: clauses with except Exception:.

Why

Bare except: catches BaseException, including KeyboardInterrupt and SystemExit, which can prevent clean process shutdown and mask critical errors. Using except Exception: catches all application-level errors while allowing system-level exceptions to propagate correctly.

Bare `except:` catches BaseException including KeyboardInterrupt and
SystemExit. Replaced 4 instances with `except Exception:`.
@haosenwang1018 haosenwang1018 requested a review from a team as a code owner February 25, 2026 13:36
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py scripts create temporary files that are not cleaned up, leading to resource leaks.
  • test_v1_persistence.py has an incorrect type hint for a method's return value.
  • test_v2_persistence.py has a bug that would cause a NameError during 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)

critical

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)

critical

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)

medium

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)

medium

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)

medium

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)

medium

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]":

@ray-gardener ray-gardener bot added the community-contribution Contributed by the community label Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant