[Python-ideas] chdir context manager

Daniel Shahaf d.s at daniel.shahaf.name
Sun Jan 20 05:25:55 CET 2013


Christian Heimes wrote on Sat, Jan 19, 2013 at 16:40:32 +0100:
> Am 19.01.2013 16:27, schrieb Calvin Spealman:
> > -1 from me, as well. Encouraging a bad habit.
> 
> It's not just bad habit. It's broken by design because it's a major race
> condition.

A couple of other clarifications: Christian clarified on IRC that this
refers to the use chdir() (which modifies process-global state) in
multithreaded applications.  The code uses fchdir so it's not vulnerable
to race conditions whereby another process removes the original cwd
before it chdir's back to it.  (In that respect it's better than the
common "try: getcwd() finally: chdir()" pattern.)

Someone suggested adding a mutex to the saved_cwd context manager.  That
would solve the race condition, but I don't have a use-case for it ---
precisely because I can't imagine multithreaded code where threads
depend their cwd.



More information about the Python-ideas mailing list