Spaces:
Running
Running
Wana see something funny? Try this exact prompt.
#2
by darkmatter2222 - opened
This cache is meant to be thread-safe and evict least-recently-used entries. It
passes single-threaded tests and passes a load test with 8 threads doing random
gets. In production under heavy concurrent writes it occasionally returns a
value for a key that was never inserted.
Find the bug, explain the exact interleaving that causes it, and give the
minimal fix.
```python
class LRUCache:
def __init__(self, capacity):
self.capacity = capacity
self.data = {}
self.order = []
self.lock = threading.Lock()
def get(self, key):
with self.lock:
if key not in self.data:
return None
self.order.remove(key)
self.order.append(key)
return self.data[key]
def put(self, key, value):
with self.lock:
if key in self.data:
self.order.remove(key)
elif len(self.data) >= self.capacity:
oldest = self.order.pop(0)
del self.data[oldest]
self.order.append(key)
self.data[key] = value
She broke! https://huggingface.co/poolside/Laguna-S-2.1-NVFP4/discussions/16