#23642: LocMemCache.incr is not thread safe
-------------------------------------+-------------------------------------
     Reporter:  tchaumeny            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Core (Cache system)  |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
    Has patch:  0                    |  Unreviewed
  Needs tests:  0                    |      Needs documentation:  0
Easy pickings:  0                    |  Patch needs improvement:  0
                                     |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by tchaumeny):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> `LocMemCache` is documented as being thread-safe, but its `incr`
> operation isn't: there is not lock wrapping the whole get+set operation.
>
> The code below demonstrates the issue:
>
> {{{
> import threading
>
> from django.core.cache.backends.locmem import LocMemCache
>
> cache = LocMemCache('test', {})
>
> cache.set('foo', 0)
>
> def increment(key):
>     cache.incr(key)
>
> threads = []
> for i in range(20):
>     t = threading.Thread(target=increment, args=('foo',))
>     t.start()
>     threads.append(t)
>
> for t in threads:
>     t.join()
>
> print cache.get('foo')   # Random results — 20 is expected
> }}}

New description:

 `LocMemCache` is documented as being thread-safe, but its `incr` operation
 isn't: there is no lock wrapping the whole get+set operation.

 The code below demonstrates the issue:

 {{{
 import threading

 from django.core.cache.backends.locmem import LocMemCache

 cache = LocMemCache('test', {})

 cache.set('foo', 0)

 def increment(key):
     cache.incr(key)

 threads = []
 for i in range(20):
     t = threading.Thread(target=increment, args=('foo',))
     t.start()
     threads.append(t)

 for t in threads:
     t.join()

 print cache.get('foo')   # Random results — 20 is expected
 }}}

--

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.f5895f1c79ad0f5cd9edcd68575b9529%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to