Opened 9 years ago

Closed 9 years ago

#4802 closed bug (fixed)

'current directory' is not thread local

Reported by: tanakh Owned by: simonmar
Priority: normal Milestone: 7.2.1
Component: libraries/directory Version: 7.0.1
Keywords: Cc: tanaka.hideyuki@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

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.

Change History (5)

comment:1 Changed 9 years ago by tanakh

Cc: tanaka.hideyuki@… added

comment:2 Changed 9 years ago by simonmar

Resolution: wontfix
Status: newclosed

Correct, the current directory is global process state, not thread-local state. As you say, this is the way it is in Win32 and POSIX threads too. If you're doing filesystem operations from multiple threads, then don't use the current directory, instead use absolute paths.

comment:3 Changed 9 years ago by simonmar

Resolution: wontfix
Status: closednew

I'll add something to the docs as you suggest.

comment:4 Changed 9 years ago by simonmar

Milestone: 7.0.3
Owner: set to simonmar

comment:5 Changed 9 years ago by simonmar

Resolution: fixed
Status: newclosed

Fixed:

commit d84494e2bc50fd97d78681cbfa11961b7903b93c
Author: Simon Marlow <marlowsd@gmail.com>
Date:   Mon Apr 4 08:52:00 2011 +0100

     add notes about the non-thread-localness of the current directory (#4082)
Note: See TracTickets for help on using tickets.