This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: mkdir+chdir problem in multiple threads
Type: behavior Stage:
Components: None Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: anonyprog, exarkun
Priority: normal Keywords:

Created on 2007-10-31 12:43 by anonyprog, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg56992 - (view) Author: anonyprog (anonyprog) Date: 2007-10-31 12:43
Under certain circumstances, creating a directory using os.mkdir then
immediately changing to it using os.chdir fails with a file not found
exception.

Here is some code to demonstrate that. Using the following program with
a parameter of 1 works fine. But anything greater than that and
exceptions occur in the first os.chdir until only one thread is left to
run completely.

Tested on Python 2.5.1 for Windows and Python 2.3.5 for Linux.

    # mkdirtest.py
    #
    # usage: mkdirtest <number of threads>

    import os, threading, sys

    x = int(sys.argv[1])

    class MkdirTest(threading.Thread):

        def __init__(self, t):
            threading.Thread.__init__(self)
            self.t = t
            print "new thread "+str(t)

        def run(self):
            for i in range(0,50):
                s = str(self.t)+"_"+str(i)
                print "mkdir "+s
                os.mkdir(s)
                os.chdir(s)
                os.chdir("..")
            print "end thread "+str(t)

    for t in range(0,x):
        print t
        a = MkdirTest(t)
        a.start()
msg56995 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2007-10-31 15:01
This isn't a bug in Python.  Working directory, which os.chdir modifies,
is process-global.  One of your threads makes a directory, then gets
suspended while another one makes a different directory and changes into
it, then the first tries to change into its directory and fails.
msg56996 - (view) Author: anonyprog (anonyprog) Date: 2007-10-31 15:35
Sorry for filing this as a bug, I am a fool. I should have read the
documentation for each operating systems' mkdir call more carefully.
History
Date User Action Args
2022-04-11 14:56:27adminsetgithub: 45708
2007-11-01 14:03:40gvanrossumsetstatus: open -> closed
resolution: not a bug
2007-10-31 15:35:23anonyprogsetmessages: + msg56996
2007-10-31 15:01:07exarkunsetnosy: + exarkun
messages: + msg56995
2007-10-31 12:43:32anonyprogcreate