Skip to content

Commit 97f3971

Browse files
committed
Fix thread safety bug in lazy load pattern
1 parent 566fff1 commit 97f3971

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

zxcvbn/matching.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ def get_ranked_dictionaries():
2222
# Do the expensive import here only
2323
from zxcvbn.frequency_lists import FREQUENCY_LISTS
2424

25-
# Build the dictionary once
26-
RANKED_DICTIONARIES = {}
27-
for name, lst in FREQUENCY_LISTS.items():
28-
RANKED_DICTIONARIES[name] = build_ranked_dict(lst)
25+
# Build in local scope before adding to global scope for thread safety
26+
built_dict = {name: build_ranked_dict(lst) for name, lst in FREQUENCY_LISTS.items()}
27+
RANKED_DICTIONARIES = built_dict
2928
return RANKED_DICTIONARIES
3029

3130

@@ -40,6 +39,7 @@ def wrapper(*args, **kwargs):
4039
return func(*args, **kwargs)
4140
return wrapper
4241

42+
4343
GRAPHS = {
4444
'qwerty': adjacency_graphs.ADJACENCY_GRAPHS['qwerty'],
4545
'dvorak': adjacency_graphs.ADJACENCY_GRAPHS['dvorak'],

0 commit comments

Comments
 (0)