[Django] #31989: Bug in posix implementation of django/core/files/locks.py

23 views
Skip to first unread message

Django

unread,
Sep 9, 2020, 8:10:15 AM9/9/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
----------------------------------------+------------------------
Reporter: jamercee | Owner: nobody
Type: Bug | Status: new
Component: Core (Other) | Version: 3.1
Severity: Normal | Keywords: locks
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
----------------------------------------+------------------------
The posix version of locks (the version which supports `import fcntl`) has
a bug. The code attempts to return** True** to indicate success or failure
acquiring a lock, but instead it always returns **False**. The reason is
that cpython `fcntl` module returns **None** if successful, and raises an
`OSError` to indicate failure (see
[https://docs.python.org/3/library/fcntl.html#fcntl.flock]).

Anyone interested in using the non-blocking (i.e. `locks.LOCKS_NB`)
requires a valid return value to know if they have successfully acquired
the lock.

I believe the correct implementation should be the following:


{{{
diff --git a/django/core/files/locks.py b/django/core/files/locks.py
index c46b00b905..4938347ea7 100644
--- a/django/core/files/locks.py
+++ b/django/core/files/locks.py
@@ -107,9 +107,15 @@ else:
return True
else:
def lock(f, flags):
- ret = fcntl.flock(_fd(f), flags)
- return ret == 0
+ try:
+ fcntl.flock(_fd(f), flags)
+ return True
+ except OSError:
+ return False

def unlock(f):
- ret = fcntl.flock(_fd(f), fcntl.LOCK_UN)
- return ret == 0
+ try:
+ fcntl.flock(_fd(f), fcntl.LOCK_UN)
+ return True
+ except OSError:
+ return False

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31989>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 10, 2020, 2:12:42 AM9/10/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
--------------------------------------+------------------------------------

Reporter: jamercee | Owner: nobody
Type: Bug | Status: new
Component: File uploads/storage | Version: 3.1
Severity: Normal | Resolution:
Keywords: locks | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by felixxm):

* has_patch: 1 => 0
* component: Core (Other) => File uploads/storage
* stage: Unreviewed => Accepted


Comment:

Thanks for the ticket. Would you like to prepare a pull request? (tests
are also required).

--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:1>

Django

unread,
Sep 11, 2020, 6:26:57 AM9/11/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: File | Version: 3.1
uploads/storage |

Severity: Normal | Resolution:
Keywords: locks | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13410 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:2>

Django

unread,
Sep 14, 2020, 2:39:19 AM9/14/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution:
Keywords: locks | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:3>

Django

unread,
Sep 14, 2020, 5:04:22 AM9/14/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution:
Keywords: locks | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* needs_better_patch: 1 => 0
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:4>

Django

unread,
Sep 14, 2020, 6:16:04 AM9/14/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned
Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution:
Keywords: locks | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:5>

Django

unread,
Sep 15, 2020, 4:55:41 AM9/15/20
to django-...@googlegroups.com
#31989: Bug in posix implementation of django/core/files/locks.py
-------------------------------------+-------------------------------------
Reporter: jamercee | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: File | Version: 3.1
uploads/storage |
Severity: Normal | Resolution: fixed

Keywords: locks | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"7be6a6a4d665061e8bc6a741b16ff92353f5d19e" 7be6a6a4]:
{{{
#!CommitTicketReference repository=""
revision="7be6a6a4d665061e8bc6a741b16ff92353f5d19e"
Fixed #31989 -- Fixed return value of
django.core.files.locks.lock()/unlock() on POSIX systems.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31989#comment:6>

Reply all
Reply to author
Forward
0 new messages