Project-MONAI / Project-MONAI/MONAI

ResourceWarning: Implicitly cleaning up TemporaryDirectory

Open
#7,839 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.7k
Forks
1.6k
Avg merge
5d 1h
Merged PRs (30d)
20

Description

Describe the bug
A ResourceWarningwarning is raised by python as a temporary file seems not to be cleaned properly when importing Monai.

To Reproduce

  1. conda create -n monai-env python=3.11 -y
    conda activate monai-env
    
  2. pip install monai[nibabel,einops]~=1.3.1
    
  3. python -W error -c "import sys; sys.tracebacklimit=100; import monai"
    

Expected behavior
Monai should import itself without triggering warnings

Environment

================================
Printing MONAI config...
================================
MONAI version: 1.3.1
Numpy version: 1.26.4
Pytorch version: 2.3.0
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: 96bfda00c6bd290297f5e3514ea227c6be4d08b4
MONAI __file__: /export/livia/home/vision/<username>/anaconda3/envs/fseft/lib/python3.11/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: 0.5.0.post2
ITK version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 5.2.1
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
scipy version: 1.13.0
Pillow version: 10.3.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: NOT INSTALLED or UNKNOWN VERSION.
tqdm version: 4.66.4
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.8
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: 0.8.0
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.
clearml version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies


================================
Printing system config...
================================
System: Linux
Linux version: Ubuntu 22.04.4 LTS
Platform: Linux-5.15.0-112-generic-x86_64-with-glibc2.35
Processor: x86_64
Machine: x86_64
Python version: 3.11.9
Process name: pt_main_thread
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 10
Num logical CPUs: 20
Num usable CPUs: 20
CPU usage (%): [74.2, 3.4, 3.7, 8.7, 3.4, 3.8, 3.4, 4.9, 3.4, 4.2, 3.7, 3.4, 3.4, 3.0, 3.4, 2.6, 2.6, 3.0, 3.0, 3.0]
CPU freq. (MHz): 3461
Load avg. in last 1, 5, 15 mins (%): [4.6, 4.6, 7.2]
Disk usage (%): 99.8
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 125.5
Available memory (GB): 119.7
Used memory (GB): 4.7

================================
Printing GPU config...
================================
Num GPUs: 2
Has CUDA: True
CUDA version: 11.8
cuDNN enabled: True
NVIDIA_TF32_OVERRIDE: None
TORCH_ALLOW_TF32_CUBLAS_OVERRIDE: None
cuDNN version: 8700
Current device: 0
Library compiled for CUDA architectures: ['sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'sm_37', 'sm_90', 'compute_37']
GPU 0 Name: NVIDIA GeForce RTX 3090
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 82
GPU 0 Total memory (GB): 23.7
GPU 0 CUDA capability (maj.min): 8.6
GPU 1 Name: NVIDIA GeForce RTX 3090
GPU 1 Is integrated: False
GPU 1 Is multi GPU board: False
GPU 1 Multi processor count: 82
GPU 1 Total memory (GB): 23.7
GPU 1 CUDA capability (maj.min): 8.6

Additional context
The import result in the following warning:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File ".../lib/python3.11/site-packages/monai/__init__.py", line 58, in <module>
    load_submodules(sys.modules[__name__], False, exclude_pattern=excludes)
  File ".../lib/python3.11/site-packages/monai/utils/module.py", line 212, in load_submodules
    importer.find_spec(name).loader.load_module(name)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 605, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 1120, in load_module
  File "<frozen importlib._bootstrap_external>", line 945, in load_module
  File "<frozen importlib._bootstrap>", line 283, in _load_module_shim
DeprecationWarning: the load_module() method is deprecated and slated for removal in Python 3.12; use exec_module() instead
Traceback (most recent call last):
  File ".../lib/python3.11/weakref.py", line 666, in _exitfunc
    f()
  File ".../lib/python3.11/weakref.py", line 590, in __call__
    return info.func(*info.args, **(info.kwargs or {}))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/tempfile.py", line 934, in _cleanup
    _warnings.warn(warn_message, ResourceWarning)
ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/tmp/tmpnybtrjkj'>

With name having the value 'monai._extensions'

Could be a duplicate of #6167, but the warning seems to be caused by monai and not pytorch if I am not mistaken.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with the reported Python command using -W error, then trace the import through monai/__init__.py, monai/utils/module.py, and the monai._extensions entry point. Compare the tempfile and weakref traceback with the import path; done means importing MONAI no longer raises the reported ResourceWarning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
developer-experience
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.