mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-18 06:24:23 +01:00
Prevent race conditions on /healthcheck (#2273)
In a high availability deployment scenario, two clients may make a request on /healthcheck at the exact same time, which can lead to check_config returning False if the second requests changes the 'healthcheck' cache key before the first one has had time to fetch the value it had set. A solution to counter this is to ensure different keys are used for each healthcheck.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from time import monotonic_ns
|
from time import time
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from sqlalchemy_utils import database_exists
|
from sqlalchemy_utils import database_exists
|
||||||
@@ -13,6 +13,7 @@ def check_database():
|
|||||||
|
|
||||||
@timed_lru_cache(timeout=30)
|
@timed_lru_cache(timeout=30)
|
||||||
def check_config():
|
def check_config():
|
||||||
value = monotonic_ns()
|
key = "healthcheck"
|
||||||
cache.set("healthcheck", value)
|
value = round(time() / 5) * 5
|
||||||
return cache.get("healthcheck") == value
|
cache.set(key, value)
|
||||||
|
return cache.get(key) == value
|
||||||
|
|||||||
Reference in New Issue
Block a user