Issue 10949: logging.RotatingFileHandler not robust enough (original) (raw)

logging.RotatingFileHandler.doRollover() can fail leaving the handler with a closed filehandle, causing all subsequent logging attempts to fail:

import logging import logging.handlers logging.getLogger().addHandler(logging.handlers.RotatingFileHandler('testlog', None, 10, 5)) logging.getLogger().setLevel(logging.INFO) logging.info('qwertyuiop') os.system('ls -l testlog*') -rw-r--r-- 1 - - 11 Jan 19 10:02 testlog -rw-r--r-- 1 - - 0 Jan 19 10:02 testlog.1 0 os.remove('testlog') logging.info('qwertyuiop') Traceback (most recent call last): File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 72, in emit self.doRollover() File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 129, in doRollover os.rename(self.baseFilename, dfn) OSError: [Errno 2] No such file or directory logging.info('qwertyuiop') Traceback (most recent call last): File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 71, in emit if self.shouldRollover(record): File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 145, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature ValueError: I/O operation on closed file logging.info('qwertyuiop') Traceback (most recent call last): File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 71, in emit if self.shouldRollover(record): File "/home8/taraniso/opt/lib/python2.6/logging/handlers.py", line 145, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature ValueError: I/O operation on closed file

Fix seems trivial enough, attaching. This is against 2.6.5, browsing subversion online seems to indicate it is needed on the trunk as well, although it won't apply cleanly.