Opened 10 years ago

Last modified 10 years ago

#23642 closed Bug

LocMemCache.incr is not thread safe — at Version 1

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 (1)

comment:1 by Thomas C, 10 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top