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
logging.config.dictConfig with file handler leaks resources #78159
Comments
Calling logging.config.dictConfig several times with a file handler in the same Python process leaks resources. INPUT: $ python3 -Wall <<EOF
import logging.config config = {
"version": 1,
"handlers": {
"file": {
"class": "logging.FileHandler",
"filename": "test.log"
}
},
"root": {
"handlers": ["file"]
}
}
logging.config.dictConfig(config)
logging.config.dictConfig(config)
EOF OUTPUT: /usr/lib/python3.6/logging/config.py:789: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/maggyero/test.log' mode='a' encoding='UTF-8'> |
I think while initializing it on the second call we just delete the handlers list variable which contains references to the file handlers instead of properly closing them like it's done in shutdown. handlers is a list of weakreferences and I don't know if they will close file handlers or other resources automatically as they are deleted. If the initialization is done in a loop involving threads then there might be others who are using the handlers as we delete them. The attached patch tries to close the handlers before deletion thus removing the warning. The same is observed in FileConfig too where handlers list is deleted before removal. shutdown method : https://github.com/python/cpython/blob/master/Lib/logging/__init__.py#L1994 I think this is merged from Python 2.7 with commit 00ee7ba 12 years back but doesn't produce any effect on Python 2.7 does -W all or -Wall have any effect ? Thanks |
This seems to be a related issue https://bugs.python.org/issue23010 and fix attached but I think it's more towards file handler to be not opened until it's required. Thanks |
Thanks much @maggyero for the original report and sample test case this was my first non-easy bug fix. Thanks @vinay.sajip for the guidance on logging module implementation. Thanks |
Thanks @XTreak and @vinay.sajip for the fix. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: