Skip to content
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

Closed
Shufla mannequin opened this issue Nov 19, 2010 · 5 comments
Closed

Handler.close is not called in subclass while Logger.removeHandler is called #54671

Shufla mannequin opened this issue Nov 19, 2010 · 5 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Shufla
Copy link
Mannequin

Shufla mannequin commented Nov 19, 2010

BPO 10462
Nosy @vsajip, @bitdancer
Files
  • checker.py: code snippet showing
  • checkersimple.py: Simpler version, just add and remove handler
  • checkerkeyint.py: Snippet with not catched exception, which works on >=2.7
  • 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:

    assignee = None
    closed_at = <Date 2010-11-20.14:33:38.851>
    created_at = <Date 2010-11-19.17:31:59.937>
    labels = ['invalid', 'type-bug', 'library']
    title = 'Handler.close is not called in subclass while Logger.removeHandler is called'
    updated_at = <Date 2010-11-20.14:33:38.850>
    user = 'https://bugs.python.org/Shufla'

    bugs.python.org fields:

    activity = <Date 2010-11-20.14:33:38.850>
    actor = 'vinay.sajip'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-20.14:33:38.851>
    closer = 'vinay.sajip'
    components = ['Library (Lib)']
    creation = <Date 2010-11-19.17:31:59.937>
    creator = 'Shufla'
    dependencies = []
    files = ['19639', '19640', '19641']
    hgrepos = []
    issue_num = 10462
    keywords = []
    message_count = 5.0
    messages = ['121554', '121555', '121556', '121562', '121655']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'r.david.murray', 'Shufla']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue10462'
    versions = ['Python 2.7']

    @Shufla
    Copy link
    Mannequin Author

    Shufla mannequin commented Nov 19, 2010

    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.

    @Shufla Shufla mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Nov 19, 2010
    @Shufla
    Copy link
    Mannequin Author

    Shufla mannequin commented Nov 19, 2010

    Attached simpler version of checker.

    @Shufla
    Copy link
    Mannequin Author

    Shufla mannequin commented Nov 19, 2010

    Another snippet, which *WORKS* on python>=2.7, non catched exception is raised.

    @bitdancer
    Copy link
    Member

    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).

    @vsajip
    Copy link
    Member

    vsajip commented Nov 20, 2010

    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].

    @vsajip vsajip closed this as completed Nov 20, 2010
    @vsajip vsajip added the invalid label Nov 20, 2010
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants