[Zope] Cautionary tale -- OSError: [Errno 2] No such file or directory

John Ziniti jziniti@speakeasy.org
Fri, 04 Apr 2003 10:51:25 -0500


I would occasionally get these errors when trying to do things
with certain Products I had written for our website.  I had attributed
it to Zope/ZEO weirdness and would usually just restart the server
in frustration.

Recently I had the opportunity to look into one of these incidences
in more detail and found that the line it was choking on was a

os.getcwd()

It then dawned on me that we do a lot of "os.chdir()" in our Products
to make sure we are in a directory we can write in.  We also have
at least one Product that deletes it's working directory after it is done.
I imagined a situation where a Zope thread was os.chdir()ed into a
working diretcory, then the directory was deleted, leaving the Zope
thread *nowhere*.  The next time os.getcwd() was called, it raised
the above OSError ...

The morals of the story:

1)  if you use os.chdir in your Products, make  sure you os.chdir()
     *back* when you are done.

2) If you use os.getcwd() in your Products, count on the fact that
    someone else's Product did not follow #1 by catching OSError
    and picking a decent cwd.  I use the following

<code>
try: cwd = os.getcwd()
except OSError: cwd = os.environ.get('INSTANCE_HOME', '/tmp')
</code>

Hope this helps someone sometime,

John Ziniti