Repository navigation
Close temporary file descriptors and remove the unused warp matrix - #86
Open
Arthur031221 wants to merge 1 commit into
Open
Arthur031221 wants to merge 1 commit into
Arthur031221 wants to merge 1 commit into
Conversation
effigies
reviewed
Nov 6, 2026
effigies
left a comment
Contributor
There was a problem hiding this comment.
What about instead doing:
with tempfile.TemporaryDirectory(prefix='pydeface', delete=forcecleanup) as tmpdir:
if verbose:
print(f'Temporary directory: {tmpdir}')
template_reg_mat = os.path.join(tmpdir, 'reg.mat')
...There's no need to close file descriptors, because they aren't opened. The generate_tmpfiles helper goes away, and the files are cleaned up based on the forcecleanup flag.
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.
Anyone who defaces many images from one Python process (a loop over a BIDS dataset calling
deface_image) leaks four open file descriptors per image, which eventually fails withOSError: Too many open files; separately, every run leaves one temporary.matfile behind even with cleanup on.generate_tmpfilesdiscards the first value oftempfile.mkstemp, which is an open descriptor, so the four descriptors are never closed.deface_imagealso never removeswarped_mask_mat: it is not part of the return value, so neitherforcecleanup=Truenor the CLI'scleanup_files(...)call can reach it.Reproduction on current master (Linux; FSL is not installed here, so
FLIRT.runis stubbed to write a blank image and the second check only shows that the file is not removed;TMPDIRpointed at an empty directory):With this change both numbers are zero.
Changes:
generate_tmpfilescloses each descriptor right aftermkstemp; the returned paths and their order are unchanged.deface_imageremoveswarped_mask_matonce the second FLIRT call has finished.Two tests added in
tests/test_utils.py. Both fail on master (descriptor count grows; one.matfile remains) and pass here. Local run on Linux, Python 3.12:7 passed, 1 skipped(the skip istest_deface_image, which needs FSL, so nothing here ran against a real FSL install).ruff checkandruff format --checkare clean at v0.15.12, the version pinned in.pre-commit-config.yaml.Targets
master, where #83 and #85 were merged.