Opened 10 years ago

Closed 10 years ago

#23642 closed Bug (fixed)

LocMemCache.incr is not thread safe

Reported by: Thomas C Owned by: nobody
Component: Core (Cache system) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Thomas C)

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

Change History (4)

comment:1 by Thomas C, 10 years ago

Description: modified (diff)

comment:2 by Loic Bistuer, 10 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Thomas C, 10 years ago

Has patch: set

comment:4 by Loic Bistuer <loic.bistuer@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 6448dd833524ac3fc503506b624841c9d642de8a:

Fixed #23642 -- Made LocMemCache.incr() thread-safe as documented

Note: See TracTickets for help on using tickets.
Back to Top