FIX : Temporary files not cleaned after use #928
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.
This fixes issue #731
Earlier the tempfiles and directories created during activities in a project were not cleaned up and led to piling up of files causing the folder to bloat upto several gigabytes.
Changes Made:
TempfileManager
Class within utils.py which tracks newtempfile
creation and cleans them up when the the project or application is closed.clean_up_old_files()
withinTempfilManager
to check for oldertempfiles/directories
(older than 1 hour or can be changed according to suggestions) and remove them.tempfile
registration and cleanup with several project objects like masks and surfaces.NoneType
AttributeErrors
, circular import errors, project_path errors by adding checks.Features:
__init__()
initialises theTempfileManager
, starts theThreadPoolExecutor
and calls the_startup_background_cleanup()
function on application startup._startup_background_cleanup()
starts a thread for the_cleanup_old_files
to clean up oldertempfiles/directories
in the background._cleanup_old_files()
pattern matches and finds out project relatedtempfiles/directories
which are older thanOLD_FILE_THRESHOLD
from previous sessions and adds them to thecleanup_queue
.register_temp_file()
trackstempfiles
creation across various part of the codebase and adds to their reference count. The tracked files are added tocleanup_queue
once the reference count drops to zero.decrement_refs()
decreases the reference count of tracked file objects every time they are dropped or deleted._cleanup_worker()
fetches items from thecleanup_queue
and removes them using_safe_remove_file()
._safe_remove_file()
safely handles and removestempfiles
once their reference counts drop to zero i.e. they are not being used by any part of the program and properly handles errors.shutdown()
stops the running threads, callscleanup_all()
and properly shuts downTempfileManager
.cleanup_all()
cleans up any leftovertempfiles
and starts their cleanup.I have tested the changes with some basic operations like creating/removing a mask and creating/removing a surface, then saving and reopening.
It would be great if further testing is done.
Please take a look @tfmoraes @paulojamorim @rmatsuda @Rakhesis
P.S. I have commented out the logging statements, as I had added them for testing. They can be removed if needed.