Cautionary tale -- OSError: [Errno 2] No such file or directory
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
On Friday 04 April 2003 4:51 pm, John Ziniti wrote:
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.
Beware that chdir affects all threads in a process. If this method is invoked by two concurrent threads then one will not be writing in the directory that it expected.
Hope this helps someone sometime,
Indeed -- Toby Dickenson http://www.geminidataloggers.com/people/tdickenson
Beware that chdir affects all threads in a process. If this method is invoked by two concurrent threads then one will not be writing in the directory that it expected.
Gah!!!! I was *reallly* hoping not to hear that!
On Fri, 2003-04-04 at 07:51, John Ziniti wrote:
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.
<snip>
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
And the hardest lesson I've had to learn about Zope: 3. Most Zope "weirdness" is better described as "operator error" :-) Dylan
participants (3)
-
Dylan Reinhardt -
John Ziniti -
Toby Dickenson