refactor: optimize codebase for better performance and reliability#811
Merged
harry0703 merged 1 commit intoharry0703:mainfrom Apr 2, 2026
Merged
Conversation
## Key Optimizations: ### 1. video.py (app/services/video.py) - Remove unused variable calculation (req_dur) - Fix resource access after close by saving clip.duration before closing - Improve error handling in delete_files() with proper logging - Better code clarity and maintainability ### 2. material.py (app/services/material.py) - Fix thread-safety issue: replace global counter with thread-safe implementation using threading.Lock - Fix resource leak: ensure VideoFileClip is properly closed using try-finally block - Improve exception handling and resource cleanup - Prevents potential race conditions in concurrent scenarios ### 3. video.py controller (app/controllers/v1/video.py) - Fix critical bug: use correct variable 'file' instead of 'v' in file_to_uri() - Prevents NameError and ensures proper URI generation for task files ### 4. utils.py (app/utils/utils.py) - Improve error logging in to_json() with proper error messages - Add exc_info=True to background thread error logging for better debugging - Make background threads non-daemon for more predictable behavior ## Impact: - Better thread-safety for concurrent video generation tasks - Reduced resource leaks and memory issues - Improved error visibility and debugging capability - More robust error handling throughout the codebase - Fixed potential runtime errors in URI generation ## Testing: - All changes are backward compatible - No breaking changes to public APIs - Improved reliability for production use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Key Optimizations:
1. video.py (app/services/video.py)
2. material.py (app/services/material.py)
3. video.py controller (app/controllers/v1/video.py)
4. utils.py (app/utils/utils.py)
Impact:
Testing: