You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
logging reopens file with same mode, possibly truncating #86544
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:
importatexitatexit.register(lambda: logging.info("so long"))
importlogginglogging.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.
New changeset 96cf5a6 by andrei kulakov in branch 'main': bpo-42378: fixed log truncation on logging shutdown (GH-27310) 96cf5a6
DustinOprea commented on Jan 7, 2022
DustinOpreamannequin
Mannequin
More actions
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.
DustinOprea commented on Jan 7, 2022
DustinOpreamannequin
Mannequin
More actions
<- 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.
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.
Activity
ecatmur commentedon Nov 16, 2020
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:
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
akulakov commentedon Jul 23, 2021
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.
miss-islington commentedon Jul 25, 2021
New changeset 96cf5a6 by andrei kulakov in branch 'main':
bpo-42378: fixed log truncation on logging shutdown (GH-27310)
96cf5a6
DustinOprea commentedon Jan 7, 2022
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.
DustinOprea commentedon Jan 7, 2022
<- 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.
vsajip commentedon Jan 7, 2022
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.
vsajip commentedon Jan 7, 2022
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
erlend-aasland commentedon May 18, 2022
Can we close this, or is there more work left?
vsajip commentedon May 19, 2022
I think we can close it.
Update txt logger mode to 'a' (#584)
Don't let a closed file handler overwrite its file