AOSSIE-Org / AOSSIE-Org/PictoPy

Feat: Fix Model Initialization Crash Risk

Open
#1,212 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
283
Forks
679
Avg merge
7d 2h
Merged PRs (30d)
3

Description

### Describe the feature

## Model Initialization Error Handling

Both the **YOLO** and **FaceNet** classes initialize the model session without error handling.
If the model file is missing or hardware acceleration (CUDA) fails, the program crashes.

### Files
- `YOLO.py` (Model)
- `FaceNet.py` (Model)

### Issue
`onnxruntime.InferenceSession` is initialized without a `try...except` block.

### Fix
Wrap the initialization in a `try...except` block and:
- provide a fallback to `CPUExecutionProvider`, or
- log a clear error message.

### Add ScreenShots

### Affected Files
- `YOLO.py`
- `FaceNet.py`

### Description
Both model classes initialize an ONNX Runtime session directly using `onnxruntime.InferenceSession(...)` without any exception handling. Since this constructor performs model loading and execution provider initialization, any failure during this process can crash the backend server.

### Code Locations

#### 1. YOLO Model Initialization (`YOLO.py`)
```python
23: self.session = onnxruntime.InferenceSession(
24: self.model_path, providers=ONNX_util_get_execution_providers()
25: )
```

#### 2. FaceNet Model Initialization (`FaceNet.py`)
```python
14: self.session = onnxruntime.InferenceSession(
15: model_path, providers=ONNX_util_get_execution_providers()
16: )
```

### Why This Is a Risk
The `onnxruntime.InferenceSession` constructor is a **blocking operation** that attempts to:

- Load the `.onnx` model file
- Initialize execution providers (e.g., CUDA, CPU)
- Allocate required runtime resources

If anything fails during this step, an exception is raised. Common causes include:

- Missing or corrupted `.onnx` model file
- Incorrect model path
- CUDA or GPU driver incompatibility
- Failure to initialize execution providers
- Permission or filesystem issues

Since these calls are **not wrapped in a `try/except` block**, the exception propagates upward and can **crash the entire backend service during startup**.

### Impact
- Backend server may fail to start
- No graceful error reporting
- No fallback to CPU execution provider
- Reduced system reliability in production

### Record

- [x] I agree to follow this project's Code of Conduct
- [x] I want to work on this issue

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.