Skip to content

Race condition in all singleton initializations #9

Description

@DeFiVC

What

All singletons use the pattern:

global _client
if _client is None:
    _client = CohereClient()
return _client

In a concurrent async context, multiple coroutines can read _client is None as True simultaneously and each create a new instance.

Why

While only one "wins" the assignment, the others may have partially initialized state.

Scope

  • Use asyncio.Lock or module-level eager initialization

Acceptance Criteria

  • Singleton initialization is thread/async-safe

Technical Context

  • Files:
    • src/services/cohere_client.py, lines 148-151
    • src/services/embedding.py, lines 62-64
    • src/services/generation.py, lines 130-132
    • src/services/quiz_generator.py, lines 125-127
    • src/services/feedback_engine.py, lines 130-132
    • src/knowledge/vectorstore.py, lines 170-174

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions