Skip to content

bug: _GENAI_CLIENT singleton not thread-safe on cold start #82

@QuantumByte-01

Description

@QuantumByte-01

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions