Skip to content

Fix: Prevent Silent Exception Swallowing in Model Loading#530

Open
priyan17singh wants to merge 1 commit intoJdeRobot:masterfrom
priyan17singh:fix-Silent-Exception-Swallowing
Open

Fix: Prevent Silent Exception Swallowing in Model Loading#530
priyan17singh wants to merge 1 commit intoJdeRobot:masterfrom
priyan17singh:fix-Silent-Exception-Swallowing

Conversation

@priyan17singh
Copy link
Copy Markdown
Contributor

Fixes: #520

Summary

This PR fixes silent exception swallowing in model loading logic by:

  • Replacing assert statements with explicit exceptions
  • Removing broad except Exception: blocks
  • Introducing specific exception handling for TorchScript fallback
  • Preserving error context using exception chaining
  • Improving optional dependency import handling

🚨 Problem

The current implementation:

  • Uses assert for file validation (unsafe in production).
  • Uses broad except Exception: blocks.
  • Silently swallows critical errors (e.g., file not found, CUDA OOM, corrupted models).
  • Provides misleading fallback messages.

This makes debugging difficult and hides real failures.


Changes Made

1. Safer File Validation

if not os.path.isfile(model):
    raise FileNotFoundError(f"Model file not found: {model}")

2. Improved Exception Handling

  • Catch only expected exceptions during TorchScript loading
  • Fallback to PyTorch loading only when appropriate
  • Raise clear error if both loading methods fail
except (RuntimeError, EOFError) as jit_err:
    try:
        model = torch.load(model, map_location=self.device)
    except Exception as load_err:
        raise RuntimeError(
            f"Failed to load model. TorchScript error: {jit_err}, "
            f"PyTorch error: {load_err}"
        ) from load_err

3. Optional Dependency Handling

except ImportError:
    print("Optional dependency Open3D-ML3D not available")

🧪 Tests Added

Added tests/test_model_loading.py to validate error handling behavior.

✔️ Test Results

collected 4 items

test_detection_model_bad_file_raises_specific_error PASSED
test_segmentation_model_bad_file_raises_specific_error PASSED
test_detection_model_missing_file_raises_file_not_found PASSED
test_segmentation_model_missing_file_raises_file_not_found PASSED

4 passed, 1 warning in 12.76s

What is tested

  • Invalid model file → raises appropriate exception
  • Missing file → raises FileNotFoundError
  • Ensures no silent fallback occurs on real errors

📊 Impact

  • Improves debuggability
  • Prevents silent failures
  • Ensures correct error propagation
  • Aligns with Python best practices
  • Makes model loading more robust for production use

@dpascualhe
Copy link
Copy Markdown
Collaborator

Hi @priyan17singh , thanks for your contribution! Can you fix the conflicts? I'll review the PR after that

@priyan17singh priyan17singh force-pushed the fix-Silent-Exception-Swallowing branch from b45d1a6 to 28eda4c Compare April 17, 2026 14:03
@priyan17singh
Copy link
Copy Markdown
Contributor Author

Hi @dpascualhe, thank you for taking the time to review!

I've resolved the merge conflicts by rebasing my branch on top of the latest master and keeping the improved exception handling from this PR. The branch is now up to date — feel free to review when you get a chance!
Let me know if anything else needs to be addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue : Silent Exception Swallowing in Model Loading

2 participants