Problem
Two concurrent requests hitting a cold start can both see _GENAI_CLIENT is None and both initialize it:
_GENAI_CLIENT = None
def _get_genai_client():
global _GENAI_CLIENT
if _GENAI_CLIENT is None: # race condition here
_GENAI_CLIENT = genai.Client(...)
return _GENAI_CLIENT
Fix
Use asyncio.Lock or initialize the client eagerly at module load time instead of lazily.