logging reopens file with same mode, possibly truncating #86544
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
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'
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']
The text was updated successfully, but these errors were encountered:
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)
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.
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.
Python logging to a file with mode='w' can cause log loss when logging happens after the logger has already been cleaned up. See issue: python/cpython#86544 The fix for Python was to have special cases for files opened in mode='w'. Currently we use mode='w+', so the checked in fix does not solve our problem. I've implemented moving the file_logger to mode='a' and an explicit deletion of the logging file before opening the FileHandler.