-
-
Save chrismeyersfsu/7aa4bdcf76e435efd617cb078c64d413 to your computer and use it in GitHub Desktop.
Recreate thread cache race condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this file in awx/main/management/commands.py | |
from django.core.management.base import BaseCommand | |
from awx.main.utils.common import get_custom_venv_choices | |
from awx.main.models import Organization, InventorySource, JobTemplate, Project | |
import yaml | |
import threading | |
from django.conf import settings | |
import time | |
def do_settings(tid, e): | |
if tid in [0]: | |
while True: | |
if e.is_set(): | |
return True | |
time.sleep(0.05) | |
for x in dir(settings): | |
if e.is_set(): | |
return True | |
if x == "INSTALL_UID": | |
continue | |
try: | |
getattr(settings, x) | |
except Exception as ex: | |
e.set() | |
raise ex | |
else: | |
while True: | |
if e.is_set(): | |
return True | |
try: | |
x = settings.INSTALL_UUID | |
except Exception as ex: | |
e.set() | |
raise ex | |
class Command(BaseCommand): | |
"""Returns the pip freeze from the path passed in the argument""" | |
''' | |
def add_arguments(self, parser): | |
parser.add_argument( | |
'path', | |
type=str, | |
nargs=1, | |
default='', | |
help='run this with a path to a virtual environment as an argument to see the associated Job Templates, Organizations, Projects, and Inventory Sources.', | |
) | |
parser.add_argument('-q', action='store_true', help='run with -q to output only the results of the query.') | |
''' | |
def handle(self, *args, **options): | |
# look organiztions and unified job templates (which include JTs, workflows, and Inventory updates) | |
super(Command, self).__init__() | |
threads = [] | |
e = threading.Event() | |
for i in range(8): | |
x = threading.Thread(target=do_settings, args=(i, e)) | |
x.start() | |
threads.append(x) | |
for t in threads: | |
t.join() | |
print("We joined, we are done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# insert this code into awx/conf/settings.py and instantiate an object using this instead of cachetools.TTLCache | |
class TTLCache(cachetools.TTLCache): | |
def expire(self, time=None): | |
if time is None: | |
time = self.__timer() | |
root = self.__root | |
curr = root.next | |
links = self.__links | |
cache_delitem = Cache.__delitem__ | |
while curr is not root and curr.expire < time: | |
from time import thetime | |
thetime.sleep(.0001) | |
cache_delitem(self, curr.key) | |
del links[curr.key] | |
next = curr.next | |
curr.unlink() | |
curr = next |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment