[issue14632] Race condition in WatchedFileHandler leads to unhandled exception

Charles-François Natali report at bugs.python.org
Mon Apr 23 19:05:36 CEST 2012


Charles-François Natali <neologix at free.fr> added the comment:

> Charles-François: Certainly I can reduce the iterations to make the test
> faster. As it is, I did reproduce the failure on my dev system, but only
> once, after running John's test script about a dozen times: every other
> time, it completed successfully :-(

Juste reduce the sleep times, e.g.:
"""
@@ -17,7 +17,7 @@
             os.unlink(fname)
         except OSError:
             pass
-        stime = 0.04 * random.randint(0, 4)
+        stime = 0.004 * random.randint(0, 4)
         time.sleep(stime)

@@ -50,7 +50,7 @@
         log.setLevel(logging.INFO)

         for ii in range(LOGCOUNT):
-            stime = 0.05 # * random.randint(0, 4)
+            stime = 0.005 # * random.randint(0, 4)
             time.sleep(stime)
             log.warning('Foo bar %d', ii)
"""

With this change, I can trigger a failure reliably in around 1s, and
my computer is rather slow.

> Isn't it simpler if I just replace the os.path.exists() calls with
> os.stat(), and check for ENOENT if an exception of type OSError or
> WindowsError occurs?

The problem is that it would leave a race window if the file is
changed between the time it's opened (I guess in
logging.FileHandler.__init__()) and the first call to stat().
John's patch is safe in this regard, thanks to fstat().

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14632>
_______________________________________


More information about the Python-bugs-list mailing list