id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	os	architecture	failure	difficulty	testcase	blockedby	blocking	related
2134	Intermittent file locking bug in installPackage.hs	gw	duncan	"After fixing the bug in ticket #1992, I still saw lock occasional
lock errors building ghc.  Dumping all of the fd/dev/inode info in lockFile
and unlockFile showed that the lock hash tables were not inconsistent, as they
had been in #1992, but that a write lock was being placed on a file before
a read lock was released.

The culprit is the reading of the configuration in libraries/installPackage.hs
({{{lbi <- getConfig verbosity}}}).  This ultimately uses tryGetConfigStateFile from
libraries/Cabal/Distribution/Simple/Configure.hs.  And this function uses readFile
to read in the configuration file.

The dump of fd/dev/inode info showed that readFile was acquiring a lock that was
not released before the end of the program.  Occasionally, there was an attempt to
obtain a write lock on the same dev/inode, leading to a lock error.  It looks as if
this is the laziness problem with readFile noted in the IO library documentation.

Making the read of the configuration file strict and explicitly closing the handle
fixes the problem for me.  Here is the patch:

{{{
--- libraries/Cabal/Distribution/Simple/Configure.hs.sav	2008-03-01 17:01:39.000000000 -0500
+++ libraries/Cabal/Distribution/Simple/Configure.hs	2008-03-01 17:04:25.000000000 -0500
@@ -130,7 +130,7 @@
 import qualified System.Info
     ( os, arch )
 import System.IO
-    ( hPutStrLn, stderr )
+    ( hPutStrLn, stderr, openFile, IOMode(..), hGetContents, hClose )
 import Text.PrettyPrint.HughesPJ
     ( comma, punctuate, render, nest, sep )
     
@@ -146,7 +146,9 @@
   let dieMsg = ""error reading "" ++ filename ++ 
                ""; run \""setup configure\"" command?\n""
   if (not e) then return $ Left dieMsg else do 
-    str <- readFile filename
+    hdl <- openFile filename ReadMode
+    str <- hGetContents hdl >>= \s -> last s `seq` return s
+    hClose hdl
     case reads str of
       [(bi,_)] -> return $ Right bi
       _        -> return $ Left  dieMsg
}}}

I'm still not certain why the bug occurs only some of the time, and whether
it is OS dependent.  But readFile's holding of the lock for an unpredictable
time is just asking for trouble.

"	bug	closed	normal	6.8.3	libraries (other)	6.8.2	fixed		duncan.coutts@…	Unknown/Multiple	Unknown/Multiple		Easy (less than 1 hour)				
