LockFile.acquire() argument docs are incorrect

Bug #1419127 reported by Kirk Strauser
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pylockfile
Fix Released
Medium
Davanum Srinivas (DIMS)

Bug Description

According to http://pythonhosted.org/lockfile/lockfile.html#lockfile.LockFile.acquire:

> If the timeout is zero or a negative number the AlreadyLocked exception will be raised if the file is currently locked by another process or thread.

I have two Python shells open. In one, I've run:

>>> import lockfile
>>> a = lockfile.LockFile('/tmp/locker')
>>> a.acquire()

In the other, I run:

>>> import lockfile
>>> a = lockfile.LockFile('/tmp/locker')
>>> a.acquire(0) # Hangs until I ctrl-C interrupt it
>>> a.acquire(-1)
Traceback (most recent call last):
[...]
lockfile.AlreadyLocked: /tmp/locker is already locked

The docs incorrectly state that a.acquire(0) and a.acquire(-1) should have the same behavior.

Tags: small
Changed in pylockfile:
status: New → Confirmed
importance: Undecided → Medium
tags: added: small
Revision history for this message
Michael Komitee (mkomitee) wrote :

I just opened https://bugs.launchpad.net/pylockfile/+bug/1468124 before I realized it was more or less a duplicate of this issue.

I would add that this was noted and the current code looks like an attempt to fix the bug:

>>> % git show 58c3832d --pretty=full
>>> commit 58c3832
>>> Author: Skip Montanaro <email address hidden>
>>> Commit: Skip Montanaro <email address hidden>
>>>
>>> fix for timeout=0
>>>
>>> diff --git a/lockfile/linklockfile.py b/lockfile/linklockfile.py
>>> index a906568..9c50673 100644
>>> --- a/lockfile/linklockfile.py
>>> +++ b/lockfile/linklockfile.py
>>> @@ -19,7 +19,7 @@ class LinkLockFile(LockBase):
>>> except IOError:
>>> raise LockFailed("failed to create %s" % self.unique_name)
>>>
>>> - timeout = timeout or self.timeout
>>> + timeout = timeout is not None and timeout or self.timeout
>>> end_time = time.time()
>>> if timeout is not None and timeout > 0:
>>> end_time += timeout
>>> *SNIP*

I'd also mention that this is a regression introduced by ecd17a7, and so it would probably be better to fix the code than the docs.

Changed in pylockfile:
assignee: nobody → Davanum Srinivas (DIMS) (dims-v)
status: Confirmed → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to pylockfile (master)

Reviewed: https://review.openstack.org/221100
Committed: https://git.openstack.org/cgit/openstack/pylockfile/commit/?id=333cbd39a50e5908136f9270a354ff5d1f6b6956
Submitter: Jenkins
Branch: master

commit 333cbd39a50e5908136f9270a354ff5d1f6b6956
Author: Davanum Srinivas <email address hidden>
Date: Mon Sep 7 16:05:56 2015 -0400

    lockfile.acquire doesn't accept a timeout of 0

    Docs indicates that lock.acquire(timeout=0) would not block, but it
    does (for linklockfile) because 0 is not truthy, and the line:

       timeout = timeout is not None and timeout or self.timeout

    when timeout is 0 causes it to default to self.timeout, which by default is None.

    So rewrite the condition better to take into account that timeout may
    be set to 0.

    Closes-Bug: #1468124
    Closes-Bug: #1419127
    Change-Id: Idf0d00977e79661e1eafd695d6e148e5f27e1840

Changed in pylockfile:
status: In Progress → Fix Committed
Changed in pylockfile:
milestone: none → 0.11.0
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.