Skip to content
Myr

Prevent deadlock in LRUCache.setdefault

Before adoption: this commit came in with the history the project adopted, so no landing records it.

setdefault was acquiring write_lock, then calling getitem and also potentially setitem, which also both try to acquire the write lock.

authored by EtiennePelletier · committed by David Lord · parent 7113349 · Browse files · 2 files+6 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+6 added−10 removed

  • other:+6 added−10 removed

2 files+6 added−10 removed

modified:jinja2/utils.py+5 added-10 removed
353 unmodified lines
354 def setdefault(self, key, default=None):355 """Set `default` if the key is not in the cache otherwise356 leave unchanged. Return the value of this key.357 """358 self._wlock.acquire()358 try:360 try:361 return self[key]362 except KeyError:363 self[key] = default364 return default359 return self[key]360 except KeyError:361 self[key] = default362 return default365 finally:366 self._wlock.release()363364 def clear(self):365 """Clear the cache."""366 self._wlock.acquire()
63 unmodified lines
430 del self._mapping[key]431 try:432 self._remove(key)433 except ValueError:438 # __getitem__ is not locked, it might happen434 pass435 finally:436 self._wlock.release()437
35 unmodified lines
473474 __iter__ = iterkeys475476 def __reversed__(self):482 """Iterate over the values in the cache dict, oldest items477 """Iterate over the keys in the cache dict, oldest items478 coming first.479 """480 return iter(tuple(self._queue))481
163 unmodified lines
modified:CHANGES.rst+1 added
19 unmodified lines
20- Float literals can be written with scientific notation, like21 2.56e-3. :issue:`912`, :pr:`922`22- Int and float literals can be written with the '_' separator for23 legibility, like 12_345. :pr:`923`24- Fix a bug causing deadlocks in ``LRUCache.setdefault``. :pr:`1000`252627Version 2.10.228--------------
620 unmodified lines