This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging FileHandler.close should acquire its lock before closing stream
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: Adam.Ernst, python-dev, vinay.sajip
Priority: normal Keywords:

Created on 2011-03-08 19:54 by Adam.Ernst, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg130362 - (view) Author: Adam Ernst (Adam.Ernst) Date: 2011-03-08 19:54
This is my first report, apologies if I missed any tracker etiquette.

The logging module registers shutdown() to run via atexit. shutdown() calls flush() and close() on each handler.

However if a FileHandler is writing to a file while shutdown() is executing, an IOError will be raised as the file is being closed while written to. (This can happen if you use daemon threads, which can still be running while exiting.)


Traceback (most recent call last):
  File "/usr/local/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/local/lib/python2.7/logging/__init__.py", line 1616, in shutdown
    h.close()
  File "/usr/local/lib/python2.7/logging/__init__.py", line 898, in close
    self.stream.close()
IOError: close() called during concurrent operation on the same file object.


I'm not familiar with the internals of logging, but it seems that FileHandler should call self.acquire()/self.release() around its close and flush operations. Otherwise a daemon thread might be emitting a record while closing the handler.
msg130384 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-08 22:54
New changeset fc4d045e3170 by Vinay Sajip in branch '2.6':
Issue #11444: Lock handlers while flushing/closing during shutdown.
http://hg.python.org/cpython/rev/fc4d045e3170

New changeset c8b651b3a493 by Vinay Sajip in branch '2.7':
Issue #11444: Merge fix from 2.6.
http://hg.python.org/cpython/rev/c8b651b3a493

New changeset 16f8f359db9c by Vinay Sajip in branch '3.1':
Issue #11444: Lock handlers while flushing/closing during shutdown.
http://hg.python.org/cpython/rev/16f8f359db9c

New changeset 39469645857a by Vinay Sajip in branch '3.2':
Issue #11444: Merge fix from 3.1.
http://hg.python.org/cpython/rev/39469645857a

New changeset 1947cca7f583 by Vinay Sajip in branch 'default':
Issue #11444: Merge fix from 3.2.
http://hg.python.org/cpython/rev/1947cca7f583
msg130600 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-11 18:49
New changeset d6626c9fc28d by Vinay Sajip in branch '2.6':
Reverted bug fixes for #11444 (fc4d045e3170) and #11424 (b9d76846bb1c), which should not have been made in this branch.
http://hg.python.org/cpython/rev/d6626c9fc28d
History
Date User Action Args
2022-04-11 14:57:14adminsetgithub: 55653
2011-03-11 18:49:08python-devsetmessages: + msg130600
2011-03-08 23:00:36vinay.sajipsetstatus: open -> closed
assignee: vinay.sajip
resolution: fixed
versions: + Python 3.1, Python 3.2, Python 3.3
2011-03-08 22:54:33python-devsetnosy: + python-dev
messages: + msg130384
2011-03-08 21:39:23ned.deilysetnosy: + vinay.sajip
2011-03-08 19:54:33Adam.Ernstcreate