From 3f61afdc9306e1a6b7f292a457f1cb1d69dadd9a Mon Sep 17 00:00:00 2001 From: "D. Paolella" Date: Wed, 26 Oct 2022 12:34:39 +0200 Subject: [PATCH] Make ReleaseSchedule singleton class thread safe --- doozerlib/release_schedule.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/doozerlib/release_schedule.py b/doozerlib/release_schedule.py index 07fa600ea..3946b8527 100644 --- a/doozerlib/release_schedule.py +++ b/doozerlib/release_schedule.py @@ -1,5 +1,6 @@ from datetime import datetime import os +import threading import yaml @@ -13,11 +14,16 @@ class ReleaseSchedule: """ _instance = None + _lock = threading.Lock() def __new__(cls, runtime): if cls._instance is None: - cls._instance = super(ReleaseSchedule, cls).__new__(cls) - cls.initialize(runtime) + with cls._lock: + # Another thread could have created the instance before the lock was acquired + # So check that the instance is still nonexistent. + if cls._instance is None: + cls._instance = super(ReleaseSchedule, cls).__new__(cls) + cls.initialize(runtime) return cls._instance @classmethod