From e2d116fdf1fc703fab5491da3c9661489778b769 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 17 Feb 2022 14:40:07 -0500 Subject: [PATCH] Agent: Make request_cache() decorator thread-safe --- monkey/infection_monkey/utils/decorators.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/utils/decorators.py b/monkey/infection_monkey/utils/decorators.py index 7a93a7c7a..31ac0661b 100644 --- a/monkey/infection_monkey/utils/decorators.py +++ b/monkey/infection_monkey/utils/decorators.py @@ -1,3 +1,4 @@ +import threading from functools import wraps from .timer import Timer @@ -29,14 +30,16 @@ def request_cache(ttl: float): def decorator(fn): @wraps(fn) def wrapper(*args, **kwargs): - if wrapper.timer.is_expired(): - wrapper.cached_value = fn(*args, **kwargs) - wrapper.timer.set(ttl) + with wrapper.lock: + if wrapper.timer.is_expired(): + wrapper.cached_value = fn(*args, **kwargs) + wrapper.timer.set(ttl) return wrapper.cached_value wrapper.cached_value = None wrapper.timer = Timer() + wrapper.lock = threading.Lock() return wrapper