Ensure temporary files are cleaned up after upload process#95
Open
Darkshadow0409 wants to merge 1 commit intoindictechcom:masterfrom
Open
Ensure temporary files are cleaned up after upload process#95Darkshadow0409 wants to merge 1 commit intoindictechcom:masterfrom
Darkshadow0409 wants to merge 1 commit intoindictechcom:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent leaked temporary files created during /api/upload by adding a try/finally cleanup around the synchronous upload path.
Changes:
- Wrapped the synchronous (<50MB)
process_upload(...)call in atry/finally. - Added a
os.path.exists(...)check followed byos.remove(...)to delete the downloaded temp file.
Comments suppressed due to low confidence (1)
app.py:120
- This cleanup only applies to the synchronous (<50MB) path. The async Celery path still passes a temp file to
upload_image_taskandtasks.pycurrently does not delete it, so temp files can still accumulate (and failures in the task will leak too). To match the PR description (“always cleaned up”), add best-effort deletion in the Celery task (ideally in its owntry/finally) once the upload attempt finishes.
else:
# Process asynchronously using Celery
OAuthObj = {
"consumer_key": CONSUMER_KEY,
"consumer_secret": CONSUMER_SECRET,
"key": session['mwoauth_access_token']['key'],
"secret": session['mwoauth_access_token']['secret']
}
task = upload_image_task.delay(file_path, tr_filename, src_fileext, tr_endpoint, OAuthObj)
return jsonify({"success": True, "task_id": task.id}), 202
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+109
to
+110
| if os.path.exists(file_path): | ||
| os.remove(file_path) |
There was a problem hiding this comment.
The finally cleanup can raise (e.g., FileNotFoundError, PermissionError) and will override/mask the intended Flask response (including successful uploads). Consider making cleanup best-effort by catching OSError (or suppressing FileNotFoundError) and logging instead of letting exceptions propagate from the finally block.
Suggested change
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| try: | |
| if os.path.exists(file_path): | |
| os.remove(file_path) | |
| except OSError as e: | |
| logging.warning("Failed to remove temporary file %s: %s", file_path, e) |
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.
Related Task
https://phabricator.wikimedia.org/T415717
Problem
Temporary files created during the upload process are not always cleaned up, especially in failure scenarios. This can lead to storage issues and resource leaks over time.
Solution
This PR ensures that temporary files are always cleaned up after the upload process by using a finally block.
Changes made:
Benefits