[GHC] #10731: System.IO.openTempFile is not thread safe on Windows

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: | Owner: NeilMitchell | Type: bug | Status: new Priority: normal | Milestone: Component: | Version: 7.10.1 libraries/base | Keywords: | Operating System: Windows Architecture: | Type of failure: Runtime crash Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Revisions: | -------------------------------------+------------------------------------- Given the test program: {{{#!hs 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. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 7.10.1 Resolution: | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by NeilMitchell): Note that the exceptions start happening within seconds - this doesn't seem to be a difficult race condition to hit. I'm on Windows 8.1, but it reproduces just as easily with Windows 7. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 7.10.1 Resolution: | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by NeilMitchell): I've avoided this issue in the extra package (https://github.com/ndmitchell/extra/blob/master/src/System/IO/Extra.hs) with three techniques: * To avoid races within a process, I have a single IORef which I increment to find unique names. That guarantees that a process will never clash with itself. * To help avoid cross-process races I start that IORef at a random starting point (using the start time plus Process Id would be ideal, but I couldn't get that easily, so I use the time). * If all else fails, I retry up to 5 times on IO errors. With those features, I've yet to get a failure. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 7.10.1 Resolution: | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by rwbarton): * cc: Phyx- (added) Comment: Phyx-, do you know of a threadsafe way to create and open a temporary file on Windows? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 7.10.1 Resolution: | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Phyx-): [https://msdn.microsoft.com/en- us/library/windows/desktop/aa364991(v=vs.85).aspx GetTempFileName] with `uUnique` 0 should be thread safe. The call will do most of the steps described in comment:2. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: (none) Type: bug | Status: patch Priority: normal | Milestone: Component: libraries/base | Version: 7.10.1 Resolution: | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4278 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Phyx-): * status: new => patch * differential: => Phab:D4278 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10731: System.IO.openTempFile is not thread safe on Windows
-------------------------------------+-------------------------------------
Reporter: NeilMitchell | Owner: (none)
Type: bug | Status: patch
Priority: normal | Milestone:
Component: libraries/base | Version: 7.10.1
Resolution: | Keywords:
Operating System: Windows | Architecture:
| Unknown/Multiple
Type of failure: Runtime crash | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D4278
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Ben Gamari

#10731: System.IO.openTempFile is not thread safe on Windows -------------------------------------+------------------------------------- Reporter: NeilMitchell | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: 8.4.1 Component: libraries/base | Version: 7.10.1 Resolution: fixed | Keywords: Operating System: Windows | Architecture: | Unknown/Multiple Type of failure: Runtime crash | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Phab:D4278 Wiki Page: | -------------------------------------+------------------------------------- Changes (by bgamari): * status: patch => closed * resolution: => fixed * milestone: => 8.4.1 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10731#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC