Skip to content

Commit

Permalink
Merge pull request #430 from Unrud/patch-2
Browse files Browse the repository at this point in the history
Protect tzical cache with a lock (fixes #428)
  • Loading branch information
pganssle committed Jul 29, 2017
2 parents 7f9d40a + ca5030b commit 94f1639
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions dateutil/tz/tz.py
Expand Up @@ -15,6 +15,7 @@
import bisect

from six import string_types
from six.moves import _thread
from ._common import tzname_in_python2, _tzinfo
from ._common import tzrangebase, enfold
from ._common import _validate_fromutc_inputs
Expand Down Expand Up @@ -1051,6 +1052,7 @@ def __init__(self, tzid, comps=[]):
self._comps = comps
self._cachedate = []
self._cachecomp = []
self._cache_lock = _thread.allocate_lock()

def _find_comp(self, dt):
if len(self._comps) == 1:
Expand All @@ -1059,7 +1061,9 @@ def _find_comp(self, dt):
dt = dt.replace(tzinfo=None)

try:
return self._cachecomp[self._cachedate.index((dt, self._fold(dt)))]
with self._cache_lock:
return self._cachecomp[self._cachedate.index(
(dt, self._fold(dt)))]
except ValueError:
pass

Expand All @@ -1085,12 +1089,13 @@ def _find_comp(self, dt):
else:
lastcomp = comp[0]

self._cachedate.insert(0, (dt, self._fold(dt)))
self._cachecomp.insert(0, lastcomp)
with self._cache_lock:
self._cachedate.insert(0, (dt, self._fold(dt)))
self._cachecomp.insert(0, lastcomp)

if len(self._cachedate) > 10:
self._cachedate.pop()
self._cachecomp.pop()
if len(self._cachedate) > 10:
self._cachedate.pop()
self._cachecomp.pop()

return lastcomp

Expand Down

0 comments on commit 94f1639

Please sign in to comment.