-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-33978: close resources before deletion of logging handlers #8008
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
bpo-33978: close resources before deletion of logging handlers #8008
Conversation
Windows builds for |
Lib/logging/config.py
Outdated
@@ -74,6 +74,10 @@ def fileConfig(fname, defaults=None, disable_existing_loggers=True): | |||
logging._acquireLock() | |||
try: | |||
logging._handlers.clear() | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 3 lines
logging._handlers.clear()
logging.shutdown(logging._handlerList[:])
del logging._handlerList[:]
are now in two places - at line 76 and line 531. I suggest adding a module-level function _clearExistingHandlers()
which does these operations, along with two calls to them from those locations.
Also, logging.shutdown()
takes _handlerList
as a default argument, so it is not necessary to pass the argument to it.
@@ -0,0 +1,2 @@ | |||
Close resources before deleting logging handlers. Patch by Karthikeyan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to "Closed existing logging handlers before reconfiguration via fileConfig and dictConfig".
Lib/test/test_logging.py
Outdated
keys=root | ||
|
||
[handlers] | ||
keys=file_handler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could just name as file
rather than file_handler
. The section below then becomes [handler_file]
rather than [handler_file_handler]
.
Lib/test/test_logging.py
Outdated
def test_config8_ok(self): | ||
with self.check_no_resource_warning(): | ||
with tempfile.NamedTemporaryFile() as f: | ||
self.apply_config(self.config8.format(tempfile=f.name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do the formatting just once to a local string, and then use that.
Lib/test/test_logging.py
Outdated
@@ -1442,6 +1464,12 @@ def test_config7_ok(self): | |||
# Original logger output is empty. | |||
self.assert_log_lines([]) | |||
|
|||
def test_config8_ok(self): | |||
with self.check_no_resource_warning(): | |||
with tempfile.NamedTemporaryFile() as f: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the approach used elsewhere in this file with fd, fn = tempfile.mkstemp(".log", "test_logging-X-")
followed by closing the fd
. This allows tracing of where the temp file came from, in the odd case where it isn't deleted for some reason. It should also ensure that the file is writable, or fail at this point rather than later.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be poked with soft cushions! |
I have made the requested changes; please review again |
Thanks for making the requested changes! @vsajip: please review the changes made to this pull request. |
The windows build fails due to the below :
I don't have a windows machine to test this and if mkstemp failure is due to the name being already present then I don't know why I could see it in other tests like test_tempfile.py. Perhaps there is a better way to test this I am not aware of? Thanks |
@vsajip I think I have fixed the issues with respect to windows.
On an additional note I think this bug is also present in Python 2.7 and I hope a backport is feasible. Ref : Line 83 in 9b84cc8
Thanks |
Removing the file shouldn't be needed, just closing it should be enough. If you can delete the file, it means it can't be held open in another process. I assume you're deleting it solely because it's a temp file that's no longer needed. |
Sorry, to be clear I am not deleting the file in logging handler I am talking about the test case related scenario in mkstemp where the file is not deleted automatically and the user has to delete the file on his own. Since the file is used only by the single test process then I think the file has to be deleted in the test case at the end in the cleanup method. Correct me if I am wrong or misunderstood your reply. Thanks |
Thanks for your work on this. |
Sure, thanks much for your review comments and guidance. Does this need to be backported to 3.7, 3.6 and 2.7? |
Thanks @tirkarthi for the PR, and @vsajip for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
GH-8044 is a backport of this pull request to the 3.7 branch. |
…pythonGH-8008) (cherry picked from commit 087570a) Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
Thanks @tirkarthi for the PR, and @vsajip for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6. |
Thanks @tirkarthi for the PR, and @vsajip for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
Sorry, @tirkarthi and @vsajip, I could not cleanly backport this to |
…ation. (pythonGH-8008). (cherry picked from commit 087570a) Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
GH-8045 is a backport of this pull request to the 3.6 branch. |
Don't you only want to do this if This change is causing the socket on configured SysLogHandlers to be closed, throwing an error when they're used, for example: import logging.config
import socket
from logging.handlers import SysLogHandler
log = logging.getLogger("logtest")
syslog_handler = SysLogHandler(("127.0.0.1", 12345), socktype=socket.SOCK_DGRAM)
log.addHandler(syslog_handler)
log.warning("Works")
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
}
)
log.warning("Breaks") Causes:
It's a valid case where multiple calls to |
Close the file handlers before deleting the logging handlers so that there is no resource warning. Since the handlers are a weakreference I tried to close them by calling
self.close
on__del__
but not all handlers have thelock
attribute which resulted inAttributeError
.shutdown
handles theAttributeError
and other scenarios but it will be a much cleaner if we close the handlers during weakref deletion instead of calling shudown which might miss some places.Additionally shutdown is also registered at exit which should clean up the current handlers I hope but doesn't handle the ones that were deleted.
Attached tests and a NEWS entry. Kindly correct me if I am wrong on the approach since this is my first non-easy PR and I would like some help on this fix hoping the fix will not be much complex.
Thanks
https://bugs.python.org/issue33978