From 325567c80b5abbedf624d9428646ebca3bce20e8 Mon Sep 17 00:00:00 2001 From: "Wei-Chun, Chang" Date: Tue, 17 Oct 2023 15:07:43 +0800 Subject: [PATCH] Add thread lock to load user profile Signed-off-by: Wei-Chun, Chang --- piperider_cli/event/__init__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/piperider_cli/event/__init__.py b/piperider_cli/event/__init__.py index 4c2a41e54..2313ec031 100644 --- a/piperider_cli/event/__init__.py +++ b/piperider_cli/event/__init__.py @@ -1,4 +1,5 @@ import os +import threading import uuid from typing import Union @@ -15,6 +16,7 @@ _collector = Collector() _yml = yaml.YAML() +user_profile_lock = threading.Lock() def init(): @@ -27,15 +29,17 @@ def init(): def load_user_profile(): - if not os.path.exists(PIPERIDER_USER_PROFILE): - user_profile = _generate_user_profile() - else: - with open(PIPERIDER_USER_PROFILE, 'r') as f: - user_profile = yaml.YAML().load(f) - if user_profile.get('user_id') is None: - user_profile = _generate_user_profile() - - return user_profile + # can be called by multiple threads from capture_exception + with user_profile_lock: + if not os.path.exists(PIPERIDER_USER_PROFILE): + user_profile = _generate_user_profile() + else: + with open(PIPERIDER_USER_PROFILE, 'r') as f: + user_profile = _yml.load(f) + if user_profile.get('user_id') is None: + user_profile = _generate_user_profile() + + return user_profile def update_user_profile(update_values):