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
Handler.close is not called in subclass while Logger.removeHandler is called #54671
Comments
Attached file produces output in MyHandler.close on python2.6.6, which is expected. But on python 2.7 and 2.7.1rc1 it produces nothing, so my handler's close method is not called. |
Attached simpler version of checker. |
Another snippet, which *WORKS* on python>=2.7, non catched exception is raised. |
I doubt that close is ever called when removehandler is called. That doesn't strike me as sensible semantics. I suspect that what is happening is that in 2.6 calling removehandler removed all references to the handler, and python's garbage collection called the close method during cleanup. In 2.7 something is probably still holding a reference; I know Vinay made a number of changes in how handlers are handled in 2.7/3.2. I think Vinay will want to take a look since (assuming I am correct) the holding of the reference may be a bug, so I'm adding him to nosy. However, you shouldn't depend on close getting called after the removehandler, since not all Python implementations do garbage collection/cleanup in the same way. You should always explicitly call close when you are done with a resource. Finally, I don't understand the point of your third example (but I haven't run it). |
The reason for this behaviour is as follows: the internal list of handlers now uses weak references, see http://bugs.python.org/issue6615#msg95691 If there are no references to a handler, it can disappear at any time: and if it does, then it won't get closed at shutdown. If there are references to it, then the shutdown code will close all handlers on application exit. So: either leave references to your handlers, or close them when you're done with them, or add a __del__() which will call close() when the handler is garbage collected. BTW removeHandler() does not call close(), and never has - these are two distinct operations. It wouldn't be right for the logging package to automatically call close() on handlers, except at application exit - where the main reason for doing so is to flush any pending output and release any resources used by the handler. Note that adding a __del__(self): self.close() method to MyHandler results in the close() being called [during garbage collection]. |
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: