AOSSIE-Org / AOSSIE-Org/PictoPy

BUG: Resource leak on partial failure in image_util_classify_and_face_detect_images

Đang mở
#1,160 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Python
Star
283
Fork
679
Merge trung bình
7 ngày 2 giờ
Pull request đã merge (30 ngày)
3

Mô tả

### Is there an existing issue for this?

- [x] I have searched the existing issues

### What happened?

ObjectClassifier and FaceDetector are initialized outside the try...finally block in
backend/app/utils/images.py. This can lead to resource leaks when initialization or cleanup fails.

**Details**

**Current pattern:**
```
object_classifier = ObjectClassifier()
face_detector = FaceDetector()
try:
...
finally:
object_classifier.close()
face_detector.close()

```

**Problems:**

If FaceDetector() raises during init, finally is never executed → ObjectClassifier is not closed.
If object_classifier.close() raises, face_detector.close() is skipped.

**Impact:**

- ONNX session / memory leaks
- Potential file handle leaks
- Increased risk of backend instability during AI Tagging

**Proposed Fix**

Refactor to:
- Initialize both objects as None
- Move initialization inside try (or use nested try...finally)
- Guard each close() call (e.g. if obj is not None)
- Optionally isolate close() calls to avoid cascading failures

**Testing**
Manual: verify AI Tagging still works; simulate model load failure
Automated (optional): mock init failure and assert close() is called

**Request:**
Happy to submit a PR with the fix + test if this approach is acceptable.

### Record

- [x] I agree to follow this project's Code of Conduct

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.