Skip to content

logging reopens file with same mode, possibly truncating #86544

Closed

Description

@ecatmur
mannequin
BPO 42378
Nosy @vsajip, @miss-islington, @ecatmur, @akulakov
PRs
  • bpo-42378: fixed log truncation on logging shutdown #27310
  • [3.10] bpo-42378: fixed log truncation on logging shutdown (GH-27310) (GH-30468) #30468
  • 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 = None
    created_at = <Date 2020-11-16.19:48:22.970>
    labels = ['3.8', 'type-bug', 'library']
    title = 'logging reopens file with same mode, possibly truncating'
    updated_at = <Date 2022-01-07.22:15:34.327>
    user = 'https://github.com/ecatmur'

    bugs.python.org fields:

    activity = <Date 2022-01-07.22:15:34.327>
    actor = 'vinay.sajip'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-11-16.19:48:22.970>
    creator = 'ecatmur2'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42378
    keywords = ['patch']
    message_count = 7.0
    messages = ['381146', '398078', '398195', '410030', '410031', '410043', '410045']
    nosy_count = 5.0
    nosy_names = ['vinay.sajip', 'Dustin.Oprea', 'miss-islington', 'ecatmur2', 'andrei.avk']
    pr_nums = ['27310', '30468']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue42378'
    versions = ['Python 3.8']

    Activity

    1. ecatmur commented on Nov 16, 2020

      ecatmurmannequin
      MannequinAuthor

      If a logging.FileHandler is configured with mode='w', or if logging.basicConfig is called with filemode='w' (as suggested by the Basic Logging Tutorial https://docs.python.org/3/howto/logging.html#logging-basic-tutorial)
      and if some code logs during shutdown, after logging has closed its handlers (asyncio is prone to do this, e.g.),
      then logging.FileHandler._open will reopen the log file with the same mode as it was originally opened with, potentially truncating it and losing valuable information:

      import atexit
      atexit.register(lambda: logging.info("so long"))
      import logging
      logging.basicConfig(filename='test.log', filemode='w', level=logging.INFO)
      logging.info("super important stuff")
      $ python truncate.py
      $ cat test.log
      INFO:root:so long

      I have a hunch that the fix to bpo-26789 will potentially make things worse, as previously at least there was a chance that logging had been fully unloaded so that the call to open would fail.

      https://stackoverflow.com/questions/39838616/why-is-python-logging-framework-losing-messages

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Nov 16, 2020
    3. akulakov commented on Jul 23, 2021

      @akulakov
      Contributor

      I've put up a PR here: https://github.com/python/cpython/pull/27310/files

      If this looks good, I will update the docs and add news entry.

    4. miss-islington commented on Jul 25, 2021

      @miss-islington
      Contributor

      New changeset 96cf5a6 by andrei kulakov in branch 'main':
      bpo-42378: fixed log truncation on logging shutdown (GH-27310)
      96cf5a6

    5. DustinOprea commented on Jan 7, 2022

      DustinOpreamannequin
      Mannequin

      I believe I'm seeing this, still, in an async situation. It seems like the obvious culprit.

      When will this go out in a release? I'm on 3.10.1 from December (under Arch). The PR got merged to master in July but I went through all changelogs back to March and don't see it listed.

    6. DustinOprea commented on Jan 7, 2022

      DustinOpreamannequin
      Mannequin

      <- I'm intentionally using mode 'w' (to support development) and it's never been an issue until I recently refactored the project to be asynchronous. Now, every time I fail, I suddenly lose the logs. Not awesome.

    7. vsajip commented on Jan 7, 2022

      @vsajip
      Member

      I guess it got missed during the 3.10 beta cycle by not being backported - it might have needed to be cherry-picked. I'll see about getting it backported to 3.10.

    8. vsajip commented on Jan 7, 2022

      @vsajip
      Member

      New changeset e35430b by Miss Islington (bot) in branch '3.10':
      [3.10] bpo-42378: fixed log truncation on logging shutdown (GH-27310) (GH-30468)
      e35430b

    9. transferred this issue fromon Apr 10, 2022
    10. erlend-aasland commented on May 18, 2022

      @erlend-aasland
      Contributor

      Can we close this, or is there more work left?

    11. vsajip commented on May 19, 2022

      @vsajip
      Member

      I think we can close it.

    12. added a commit that references this issue on Jun 9, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.8 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions