Ken Kinder's personal website

Worst lock acquisition code ever.

Courtesy of CherryPy:

while True:
    try:
        lockfd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL)
    except OSError:
        time.sleep(0.1)
    else:
        os.close(lockfd)
        break
self.locked = True

Really people, really? We silently loop until an error goes away? Really?

6 Comments to Worst lock acquisition code ever.

  1. November 26, 2008 at 11:03 AM | Permalink

    Hi Ken, glad to see you back blogging. I was surprised when my RSS reader popped up with new entries from you. It’s been a long time!

    This lock method is pretty bad I agree. I’ve seen it in C code as well, although C programmers seem a bit more likely to check for a specific error code.

    The worst/most annoying file locking bug I’ve seen is in Evolution, the email program. Their mbox update code locked the original mbox file while writing a .tmp copy with the updated contents. It then used rename() to replace the original with the updated copy. At this point, the file lock is being held on the origninal file which is now DELETED! But then their code continued on as if the mbox file was still locked.

    After I figured that one out and filed a bug report they REFUSED to apply my patch to fix it because they claimed it didn’t matter, then got abusive when I asked why they were bothing to lock the file in the first place.

    sigh.

  2. November 26, 2008 at 11:06 AM | Permalink

    Also your blog has a bug in the comments feed that I am seeing in the modem chat strings Aaron wrote. It isn’t escaping the ampersands correctly, creating invalid XML.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">