Update globals safely
The right way to update these globals is to use a lock and ensure that nobody else is updating them at the same time. Also update a temporary dictionary before setting the global one so that nobody sees partial updates to the global one. This should help fix the thread-safety of shade (and other tooling built ontop of this library). Change-Id: Ie0e0369d98ba6a01edcbf447378a786eec3f13f9
This commit is contained in:
parent
eed1cbb8cd
commit
d597ee271e
@ -14,15 +14,23 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
_json_path = os.path.join(
|
_json_path = os.path.join(
|
||||||
os.path.dirname(os.path.realpath(__file__)), 'constructors.json')
|
os.path.dirname(os.path.realpath(__file__)), 'constructors.json')
|
||||||
_class_mapping = None
|
_class_mapping = None
|
||||||
|
_class_mapping_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def get_constructor_mapping():
|
def get_constructor_mapping():
|
||||||
global _class_mapping
|
global _class_mapping
|
||||||
if not _class_mapping:
|
if _class_mapping is not None:
|
||||||
|
return _class_mapping.copy()
|
||||||
|
with _class_mapping_lock:
|
||||||
|
if _class_mapping is not None:
|
||||||
|
return _class_mapping.copy()
|
||||||
|
tmp_class_mapping = {}
|
||||||
with open(_json_path, 'r') as json_file:
|
with open(_json_path, 'r') as json_file:
|
||||||
_class_mapping = json.load(json_file)
|
tmp_class_mapping.update(json.load(json_file))
|
||||||
return _class_mapping
|
_class_mapping = tmp_class_mapping
|
||||||
|
return tmp_class_mapping.copy()
|
||||||
|
@ -14,19 +14,30 @@
|
|||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
|
|
||||||
_json_path = os.path.join(
|
_json_path = os.path.join(
|
||||||
os.path.dirname(os.path.realpath(__file__)), 'defaults.json')
|
os.path.dirname(os.path.realpath(__file__)), 'defaults.json')
|
||||||
_defaults = None
|
_defaults = None
|
||||||
|
_defaults_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def get_defaults():
|
def get_defaults():
|
||||||
global _defaults
|
global _defaults
|
||||||
if not _defaults:
|
if _defaults is not None:
|
||||||
|
return _defaults.copy()
|
||||||
|
with _defaults_lock:
|
||||||
|
if _defaults is not None:
|
||||||
|
# Did someone else just finish filling it?
|
||||||
|
return _defaults.copy()
|
||||||
# Python language specific defaults
|
# Python language specific defaults
|
||||||
# These are defaults related to use of python libraries, they are
|
# These are defaults related to use of python libraries, they are
|
||||||
# not qualities of a cloud.
|
# not qualities of a cloud.
|
||||||
_defaults = dict(
|
#
|
||||||
|
# NOTE(harlowja): update a in-memory dict, before updating
|
||||||
|
# the global one so that other callers of get_defaults do not
|
||||||
|
# see the partially filled one.
|
||||||
|
tmp_defaults = dict(
|
||||||
api_timeout=None,
|
api_timeout=None,
|
||||||
verify=True,
|
verify=True,
|
||||||
cacert=None,
|
cacert=None,
|
||||||
@ -36,6 +47,6 @@ def get_defaults():
|
|||||||
with open(_json_path, 'r') as json_file:
|
with open(_json_path, 'r') as json_file:
|
||||||
updates = json.load(json_file)
|
updates = json.load(json_file)
|
||||||
if updates is not None:
|
if updates is not None:
|
||||||
_defaults.update(updates)
|
tmp_defaults.update(updates)
|
||||||
|
_defaults = tmp_defaults
|
||||||
return _defaults.copy()
|
return tmp_defaults.copy()
|
||||||
|
Loading…
Reference in New Issue
Block a user