mailgun / expiringdict

Dictionary with auto-expiring values for caching purposes.
Apache License 2.0
344 stars 76 forks source link

Fix race-condition in .items() and .values() #22

Closed kormat closed 7 years ago

kormat commented 8 years ago

In python3.5, a race-condition in both .items() and .values() can be triggered. Here's an example script:

import expiringdict
import time

d = expiringdict.ExpiringDict(10, 1)
d[1] = 1
time.sleep(0.5)
d[2] = 2
time.sleep(0.7)
print(d.items())

Here's what the race-condition looks like:

Traceback (most recent call last):
  File "./test.py", line 11, in <module>
    print(d.items())
  File "/home/kormat/expiringdict/__init__.py", line 115, in items
    for key in self:
RuntimeError: OrderedDict mutated during iteration

The fix is very simple - make a list of the keys before iterating.


This change is Reviewable

horkhe commented 7 years ago

Fixed in #28