Skip to content

Permission denied error with runProcess/openFile

Given this program:

module Main() where

import Control.Concurrent
import System.IO
import System.Process

main = do
    hSetBuffering stdout NoBuffering
    forkIO $ f "foo1.txt"
    forkIO $ f "foo2.txt"
    threadDelay $ 100*1000000
    putStrLn "Finished successfully"

f file = do
    h <- openFile file AppendMode
    hPutStrLn h "fakdjsklj"
    putChar '.'
    pid <- runProcess "sh" ["-c","sleep 0.1s"] Nothing Nothing Nothing (Just h) (Just h)
    waitForProcess pid
    f file

Running under Cygwin, in GHC 6.10.2, I get:

$ runhaskell Test.hs
..Test.hs: foo1.txt: openFile: permission denied (Permission denied)

It shouldn't - the openFile calls should always succeed. This bug is a reduced test case from a real system, which I papered over with:

retryIO :: IO a -> IO a
retryIO act = catchIO act $ \x -> do
    threadDelay $ 1 * 1000000 -- 1 second
    performGC
    act

Now calling retryIO $ openFile ... works reliably. These problems are occurring sufficiently often that retryIO is about to go in to our standard library :-)

This may be related to #2924 (closed), but has the advantage of replicating easily.

Trac metadata
Trac field Value
Version 6.10.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information