Vinay Sajip <vinay_sa...@yahoo.co.uk> added the comment:

This had dropped off my radar completely, but I still can't see where there's 
an actual bug here. This simplified script:

import logging
import sys

LOGGING = {
    'version': 1,
    'handlers': {
        'logfile': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/tmp/debug.log',
        },
    },
}

print('Starting: %s' % sys.version)
from logging.config import dictConfig
dictConfig(LOGGING)
print('After dictconfig')
print('_handlerList 1, initial:', logging._handlerList, len(logging._handlers))
import importlib
print('_handlerList 2, about to import shutil:', logging._handlerList, 
len(logging._handlers))
import shutil
print('_handlerList 3, just imported shutil:', logging._handlerList, 
len(logging._handlers))
print('')

when run with Python 3.10, produces

Starting: 3.10.0+ (heads/3.10:7203ecd332, Oct 29 2021, 10:04:19) [GCC 7.5.0]
After dictconfig
_handlerList 1, initial: [<weakref at 0x7f3e25e9e070; to 'FileHandler' at 
0x7f3e25e63dc0>] 1
_handlerList 2, about to import shutil: [<weakref at 0x7f3e25e9e070; to 
'FileHandler' at 0x7f3e25e63dc0>] 1
/home/vinay/.local/lib/python3.10/_compression.py:33: ResourceWarning: unclosed 
file <_io.FileIO name='/tmp/debug.log' mode='ab' closefd=True>
  class DecompressReader(io.RawIOBase):
ResourceWarning: Enable tracemalloc to get the object allocation traceback
_handlerList 3, just imported shutil: [] 0

But ... there are no loggers that use this handler, so the only reference would 
be the weak reference in _handlerList - it gets freed up at some point (in this 
case, when shutil is imported, but I don't believe that's necessarily relevant) 
and that causes the ResourceWarning, but where's the problem? If you either add 
a reference to the handler (by adding it to a logger) or adding "delay: True" 
to the handler configuration dict to delay opening the file, the 
ResourceWarning is no longer seen.

I tested with Python 3.4 - 3.10 and all versions behave the same way - it could 
just be down to where weak references get dumped, which is down to the vagaries 
of GC. I don't see this as a "leak" - the ResourceWarning is showing the 
developer that they opened a file for no particular reason and didn't use it.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue23010>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to