Skip to content

'current directory' is not thread local

In a threaded code, the current directory is not thread local, even using forkOS.

For example:

import System.Directory
import Control.Concurrent

main :: IO ()
main = do
  print =<< getCurrentDirectory

  forkOS $ do
    setCurrentDirectory "a"
    threadDelay $ 10^6
    print =<< getCurrentDirectory

  forkOS $ do
    threadDelay $ 10^6
    print =<< getCurrentDirectory

  threadDelay $ 20*10^6

The output is:

$ ghc --make -threaded curdir.hs
[1 of 1] Compiling Main             ( curdir.hs, curdir.o )
Linking curdir ...
$ ./curdir 
"/home/hideyuki"
"/home/hideyuki/a"
"/home/hideyuki/a"

It seems to inherit the specification of pthread. It may be environment-dependent behavior (At least, on Linux and Windows, same results are given).

If the behavior like above is the specific, documents should be added to setCurrentDirectory/getCurrentDirectory. It is very confusing.

Or if it is possible to wrap the behabior of 'current directory' to make thread local in Haskell code, I hope to make it so.

Trac metadata
Trac field Value
Version 7.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/directory
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