Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclosed file warning in GiftiImage.to_file_map #1035

Closed
HippocampusGirl opened this issue Aug 4, 2021 · 1 comment · Fixed by #1038
Closed

Unclosed file warning in GiftiImage.to_file_map #1035

HippocampusGirl opened this issue Aug 4, 2021 · 1 comment · Fixed by #1038
Labels

Comments

@HippocampusGirl
Copy link
Contributor

I get the following warning when fMRIPrep tries to write a GIfTI file

[2021-08-04 18:29:53,0633] [py.warnings     ] [WARNING  ] /usr/local/miniconda/lib/python3.7/site-packages/nibabel/filebasedimages.py:333: ResourceWarning: unclosed file <_io.BufferedWriter name='/ext/mnt/mbServerData/data/DYNAMORE/halfpipe_test-11002/nipype/fmriprep_wf/single_subject_11002_wf/func_preproc_task_stress_wf/bold_surf_wf/_target_fsaverage/update_metadata/mapflow/_update_metadata0/lh.fsaverage.gii'>
│   self.to_file_map()
│ Stack (most recent call last):
│   File "<string>", line 1, in <module>
│   File "/usr/local/miniconda/lib/python3.7/multiprocessing/forkserver.py", line 281, in main
│     old_handlers)
│   File "/usr/local/miniconda/lib/python3.7/multiprocessing/forkserver.py", line 317, in _serve_one
│     code = spawn._main(child_r)
│   File "/usr/local/miniconda/lib/python3.7/multiprocessing/spawn.py", line 118, in _main
│     return self._bootstrap()
│   File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
│     self.run()
│   File "/usr/local/miniconda/lib/python3.7/multiprocessing/process.py", line 99, in run
│     self._target(*self._args, **self._kwargs)
│   File "/usr/local/miniconda/lib/python3.7/concurrent/futures/process.py", line 239, in _process_worker
│     r = call_item.fn(*call_item.args, **call_item.kwargs)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/halfpipe/plugins/multiproc.py", line 70, in run_node
│     result["result"] = node.run(updatehash=updatehash)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/nipype/pipeline/engine/nodes.py", line 516, in run
│     result = self._run_interface(execute=True)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/nipype/pipeline/engine/nodes.py", line 635, in _run_interface
│     return self._run_command(execute)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/nipype/pipeline/engine/nodes.py", line 741, in _run_command
│     result = self._interface.run(cwd=outdir)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/nipype/interfaces/base/core.py", line 428, in run
│     runtime = self._run_interface(runtime)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/niworkflows/interfaces/surf.py", line 303, in _run_interface
│     img.to_filename(out_file)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/nibabel/filebasedimages.py", line 333, in to_filename
│     self.to_file_map()
│   File "/usr/local/miniconda/lib/python3.7/warnings.py", line 110, in _showwarnmsg
│     msg.file, msg.line)
│   File "/usr/local/miniconda/lib/python3.7/site-packages/halfpipe/logging/base.py", line 36, in showwarning
└─    logger.warning(f"{s}", stack_info=True)

The code on the fMRIPrep side looks fine (see https://github.com/nipreps/niworkflows/blob/691436a28c10aa8f1d39cbad37610220e49f3abd/niworkflows/interfaces/surf.py#L303).

As far as I can tell, this could be resolved by adding a call to close_if_mine at the end of the function.

def to_file_map(self, file_map=None):
""" Save the current image to the specified file_map
Parameters
----------
file_map : dict
Dictionary with single key ``image`` with associated value which is
a :class:`FileHolder` instance pointing to the image file.
Returns
-------
None
"""
if file_map is None:
file_map = self.file_map
f = file_map['image'].get_prepare_fileobj('wb')
f.write(self.to_xml())

@effigies effigies added the bug label Aug 4, 2021
@effigies
Copy link
Member

effigies commented Aug 4, 2021

Yup, good catch. I think we assumed the garbage collector would clean up for us... Fortunately this is a context manager:

nibabel/nibabel/openers.py

Lines 185 to 189 in 62aea04

def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close_if_mine()

So we should be able to write:

with file_map['image'].get_prepare_fileobj('wb') as f:
    f.write(self.to_xml())

Would you mind making the PR? And is there a minimal reproduction where we can always emit the warning? If so, it'd be good to have a test.

HippocampusGirl added a commit to HippocampusGirl/nibabel that referenced this issue Aug 5, 2021
HippocampusGirl added a commit to HippocampusGirl/nibabel that referenced this issue Aug 5, 2021
effigies pushed a commit that referenced this issue Feb 7, 2022
- Change as proposed by @effigies

Test for `ResourceWarning` in `GiftiImage`

Backport of gh-1038
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants