Skip to content

System.IO.openTempFile is not thread safe on Windows

Given the test program:

import Control.Concurrent
import System.IO
import Control.Monad
import System.Directory

main = do
    putStrLn "Starting"
    var <- newEmptyMVar
    tdir <- getTemporaryDirectory
    xs <- replicateM 10 $ do
        var <- newEmptyMVar
        flip forkFinally (\s -> do print s; putMVar var ()) $ do
            replicateM_ 100000 $ do
                (file, h) <- openTempFile tdir "test.txt"
                hClose h
                removeFile file
        return var
    mapM_ takeMVar xs

If I compile and run that with ghc --make TmpFile.hs -threaded && tmpfile +RTS -N5 I get:

Starting
Left C:\Users\NDMIT_~1\AppData\Local\Temp\: openTempFile: permission denied (Permission denied)
Left C:\Users\NDMIT_~1\AppData\Local\Temp\: openTempFile: permission denied (Permission denied)
...

I've reproduced this on GHC 7.8 and 7.10.1. We hit this in production about twice a day.

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