Project-MONAI / Project-MONAI/MONAI

ResourceWarning: Implicitly cleaning up

Open
#6,167 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
warning message from

python -W error -c "import monai"

with pytorch 2.0

Traceback (most recent call last):
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/weakref.py", line 642, in _exitfunc
    f()
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/weakref.py", line 566, in __call__
    return info.func(*info.args, **(info.kwargs or {}))
  File "/usr/local/anaconda3/envs/py38/lib/python3.8/tempfile.py", line 818, in _cleanup
    _warnings.warn(warn_message, ResourceWarning)
ResourceWarning: Implicitly cleaning up <TemporaryDirectory '/var/folders/6f/fdkl7m0x7sz3nj_t7p3ccgz00000gp/T/tmpqtvvcx_b'>

debugging further looks like it's triggered via importing pytorch ignite

  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/ignite/handlers/checkpoint.py(17)<module>()
-> from torch.distributed.optim import ZeroRedundancyOptimizer
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/optim/__init__.py(20)<module>()
-> from .named_optimizer import _NamedOptimizer
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/optim/named_optimizer.py(10)<module>()
-> from torch.distributed._shard.sharded_tensor import ShardedTensor
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(961)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/_shard/__init__.py(1)<module>()
-> from .api import (
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/_shard/api.py(6)<module>()
-> from torch.distributed._shard.sharded_tensor import (
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/_shard/sharded_tensor/__init__.py(9)<module>()
-> from torch.distributed._shard.partial_tensor import _PartialTensor
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/_shard/partial_tensor.py(8)<module>()
-> from torch.distributed.nn.functional import (
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(961)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/nn/__init__.py(3)<module>()
-> from .api.remote_module import RemoteModule
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/nn/api/remote_module.py(24)<module>()
-> from torch.distributed.nn.jit import instantiator
  <frozen importlib._bootstrap>(1042)_handle_fromlist()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  <frozen importlib._bootstrap>(991)_find_and_load()
  <frozen importlib._bootstrap>(975)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(671)_load_unlocked()
  <frozen importlib._bootstrap_external>(843)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  /usr/local/anaconda3/envs/py38/lib/python3.8/site-packages/torch/distributed/nn/jit/instantiator.py(19)<module>()
-> _TEMP_DIR = tempfile.TemporaryDirectory()
  /usr/local/anaconda3/envs/py38/lib/python3.8/tempfile.py(781)__init__()
-> self._finalizer = _weakref.finalize(
> /usr/local/anaconda3/envs/py38/lib/python3.8/weakref.py(519)__init__()
-> if len(args) >= 3:

(fyi @vfdev-5 )

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 python -W error -c "import monai" and inspect the import chain through PyTorch Ignite to torch.distributed.nn.jit.instantiator.py, where the temporary directory is created. Identify whether MONAI can avoid triggering the cleanup warning; done means the import no longer produces the reported ResourceWarning.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.